-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackImpl.c
97 lines (82 loc) · 1.79 KB
/
stackImpl.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <malloc.h>
/*the node to the linked list of the stack*/
typedef struct NodeStruct {
int data;
struct NodeStruct* next;
} node;
/*the struct for the stack*/
typedef struct StackStruct{
node* top;
} Stack;
/*the method to create a new node for the stack*/
node* newNode(int d){
node* p = (node*)malloc(sizeof(node));
p->next = NULL;
p->data = d;
return p;
}
/*initialize a Stack*/
Stack* initStack(){
Stack* s = (Stack*)malloc(sizeof(Stack));
s->top=NULL;
return s;
}
void destroyStack(Stack** s){
if(*s==NULL){
printf("The stack is already null\n");
return;
}
if( (*s)->top == NULL){
}
node* temp;
while( ((*s)->top)!=NULL ){
temp = (*s)->top;
(*s)->top = (*s)->top->next;
free(temp);
}
free(*s);
}
/*create a new node and push it on the stack*/
void push(Stack* s, int d){
if(s==NULL){
printf("Stack is NULL!!\n");
return;
}
node* p = newNode(d);
if(s->top==NULL) {
s->top = p;
return;
}
p->next = s->top;
s->top = p;
}
/*return the data in the node on top of the stack*/
int pop(Stack* s, int* d){
if(s==NULL) {
printf("Null stack!!\n");
return -1;
}
if(s->top==NULL) {
printf("the stack is empty\n");
return 0;
}
*d = s->top->data;
s->top = s->top->next;
return 1;
}
int main(){
Stack* stack = initStack();
push(stack, 5);
push(stack, 6);
int poppedValue=0;
int success = pop(stack, &poppedValue);
if(success>0) printf("popped value: %d\n", poppedValue);
success = pop(stack, &poppedValue);
if(success>0) printf("popped value: %d\n", poppedValue);
success = pop(stack, &poppedValue);
if(success>0) printf("popped value: %d\n", poppedValue);
destroyStack(&stack);
push(stack, 2);
return 1;
}