-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
71 lines (54 loc) · 1.03 KB
/
stack.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
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 4
struct node{
int data;
struct node *next;
};
typedef struct{
int cnt;
struct node *top;
}stack;
void initialize(stack *stk){
stk -> cnt = 0;
stk -> top = NULL;
}
void push(stack *stk, int c){
if(stk -> cnt == STACK_SIZE)
printf("Stack is full... \n\n");
else{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp -> data = c;
temp -> next = stk -> top;
stk -> top = temp;
stk -> cnt++;
}
}
int pop(stack *stk){
if(stk -> cnt == 0){
printf("\n\n\nStack is empty..\n\n");
return -1;
}
else{
struct node *temp = stk -> top;
stk -> top = stk -> top -> next;
int x = temp -> data;
free(temp);
stk -> cnt--;
return x;
}
}
main(){
stack s;
initialize(&s);
push(&s,3);
push(&s,2);
push(&s,17);
push(&s,21);
push(&s,5);
printf("%d \t", pop(&s));
printf("%d \t", pop(&s));
printf("%d \t", pop(&s));
printf("%d \t", pop(&s));
printf("%d \t", pop(&s));
}