-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
169 lines (140 loc) · 3.2 KB
/
stack.c
File metadata and controls
169 lines (140 loc) · 3.2 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "stdio.h"
#include "stdlib.h"
#include "limits.h" // For INT_MIN, see https://codeforwin.org/2018/08/stack-implementation-using-linked-list-push-pop.html
//#include "curses.h" // For clrscr, in c <conio.h>. clrscr now not used.
#define CAPACITY 3 // Only 3 nodes;
struct node{
int data;
struct node * next;
}*top;
int size=0;
void push(struct node **top, int data);
int pop(struct node **top);
int seeTop(struct node *top);
int isEmptyStack(struct node *top);
void disp(struct node *top);
void del(struct node **top);
int main(void){
int choice;
int data;
while(1){
printf("STACK IMPLEMENTATION\n");
printf("====================\n");
printf("Enter your choice:\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. TOP\n");
printf("4. SIZE\n");
printf("5. DISP\n");
printf("6. DELETE STACK\n");
printf("7. CLEAR SCREEN\n");
printf("8. EXIT\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Enter number to push:\n");
scanf("%d", &data);
push(&top,data);
break;
case 2:
data = pop(&top);
if (data!=INT_MIN)
printf("Data poped: %d.\n", data);
break;
case 3:
data = seeTop(top);
if (data!=INT_MIN)
printf("Data on top: %d.\n", data);
break;
case 4:
printf("Stack size: %d.\n", size);
break;
case 5:
printf("Stack elements:\n");
disp(top);
break;
case 6:
del(&top);
break;
case 7:
system("clear");
break;
case 8:
printf("Exiting application...\n");
exit(0); // exit is system call, so not reserved keyword, also break not needed.
// break;
default:
printf("Invalid choice, try again.\n");
// break; is not needed for default.
}
}
return 0;
}
void push(struct node **top, int data){
// Check stack overflow
if (size >= CAPACITY)
{
printf("Stack Overflow, can't add more element to stack.\n");
return;
}
struct node * temp = malloc(sizeof(struct node));
if(!temp){
printf("Memory error on creating node.\n");
return;
}
temp->data=data;
temp->next = *top;
*top=temp;
size=size+1;
return;
}
int isEmptyStack(struct node *top){
return (top==NULL || size==0);
}
int pop(struct node **top){
if(isEmptyStack(*top)){
return INT_MIN;
}
struct node *temp = *top;
*top = temp->next;
int dt = temp->data;
free(temp);
size--;
return dt;
}
int seeTop(struct node *top){
if(isEmptyStack(top)){
return INT_MIN;
}
return top->data;
}
void disp(struct node *top){
if(isEmptyStack(top)){
printf("Empty stack.\n");
}
else{
struct node * temp = top;
while(temp!=NULL){
printf("%d->", temp->data);
temp=temp->next;
}
printf("\n");
}
return;
}
void del(struct node **top){
struct node * temp, *p;
if(isEmptyStack(*top)){
printf("Empty stack.\n");
return;
}
p=*top;
while(p->next){
temp=p->next;
p->next=temp->next;
free(temp);
}
free(p);
size=0;
return;
}