-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstog_u_polju.c
80 lines (69 loc) · 1.67 KB
/
stog_u_polju.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#endif
#define SIZE 8
typedef struct {
int top;
int *array;
} Stack;
int push(Stack *stack, int val) {
if(stack->top == SIZE-1)
return -1;
stack->array[stack->top+1] = val;
(stack->top)++;
return 0;
}
int pop(Stack *stack) {
if(stack->top == -1)
return -1;
(stack->top)--;
return 0;
}
int print(Stack *stack) {
int i;
if(stack->top==-1) return -1;
for(i=0; i<=stack->top; i++) {
printf("%d ", stack->array[i]);
}
printf("\n");
return 0;
}
int main() {
int val, ret_val, menu_choice;
Stack *stack;
char c;
setbuf(stdout, NULL);
stack = (Stack *)malloc(sizeof(Stack));
stack->top = -1;
stack->array = (int *)malloc(sizeof(int)*SIZE);
do {
menu_choice = 0;
DEBUG("\n1 push\n2 pop\n3 ispis\n4 izlaz\n");
scanf("%d", &menu_choice);
switch (menu_choice) {
case 1:
scanf("%d", &val);
ret_val = push(stack, val);
if(ret_val==-1) printf("Stog je pun.\n");
break;
case 2:
ret_val = pop(stack);
if(ret_val==-1) printf("Stog je prazan.\n");
break;
case 3:
ret_val = print(stack);
if(ret_val==-1) printf("Stog je prazan.\n");
break;
case 4:
break;
default:
while((c = getchar()) != '\n' && c != EOF);
}
} while(menu_choice!=4);
free(stack->array);
free(stack);
return 0;
}