-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackasADT.c
171 lines (136 loc) · 3.13 KB
/
StackasADT.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
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
169
170
171
//Stack as an Abstract Data Type(ADT)
#include<stdio.h>
#include<string.h>
#define MAX 5
#define TRUE 1
#define FALSE 0
struct student{
char name[20];
int rn;
int age;
};
struct stack{
int TOS;
struct student s[MAX];
};
void push(struct stack *ss, struct student st){
if(ss->TOS==MAX-1){
printf("Stack Overflow\n");
}
else{
ss->TOS += 1;
ss->s[ss->TOS] = st;
}
}
struct student pop(struct stack *ss){
struct student st;
if (ss->TOS == -1)
{
printf("Stack Underflow\n");
}
else{
st = ss->s[ss->TOS];
ss->TOS -= 1;
return st;
}
}
struct student peek(struct stack *ss){
struct student st;
if (ss->TOS == -1)
{
printf("Stack Underflow\n");
}
else{
st = ss->s[ss->TOS];
return st;
}
}
int isFull(struct stack *ss){
if(ss->TOS == MAX-1){
return TRUE;
}
else{
return FALSE;
}
}
int isEmpty(struct stack *ss){
if(ss->TOS == -1){
return TRUE;
}
else{
return FALSE;
}
}
int main(){
char c;
int choice,bool;
struct student s;
struct stack stk={-1};
do{
printf("1.push\n2.pop\n3.peek\n4.isFULL\n5.isEmpty\n6.Exit\n");
printf("Enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
if(!isFull(&stk)){
printf("Enter your name\n");
scanf("%s", s.name);
printf("Enter your roll number\n");
scanf("%d", &s.rn);
printf("Enter your age\n");
scanf("%d", &s.age);
}
push(&stk, s);
break;
case 2:
if(isEmpty(&stk)){
s = pop(&stk);
printf("The removed members are\n");
printf("NAME:%s\n", s.name);
printf("Age:%d\n", s.age);
printf("Roll number:%d\n", s.rn);
}
else
{
printf("Stack Underflow\n");
}
break;
case 3:
if(isEmpty(&stk)){
s = peek(&stk);
printf("Value at the top is\n");
printf("NAME:%s\n", s.name);
printf("Age:%d\n", s.age);
printf("Roll number:%d\n", s.rn);
}
else
{
printf("Stack Underflow\n");
}
break;
case 4:
if(isFull(&stk)){
printf("Stack is Full\n");
}
else{
printf("Stack is not full\n");
}
break;
case 5:
if(isEmpty(&stk)){
printf("Stack is empty\n");
}
else{
printf("Stack is not empty\n");
}
break;
case 6:
printf("Exit");
break;
default:
printf("Choose numbers between 1 to 6\n");
};
} while (choice != 6);
return 0;
}