-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.c
188 lines (154 loc) · 3.63 KB
/
22.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// C program to Implement a stack
//using singly linked list
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define CAPACITY 10000
int size
// Declare linked list node
struct Node {
int data;
struct Node* link;
};
struct Node* top;
// Utility function to add an element data in the stack
// insert at the beginning
void push(int data)
{
// create new node temp and allocate memory
struct Node* temp;
temp = (struct Node*)malloc(sizeof(struct Node));
// check if stack (heap) is full. Then inserting an element would
// lead to stack overflow
if (!temp) {
printf("\nHeap Overflow");
exit(1);
}
// initialize data into temp data field
temp->data = data;
// put top pointer reference into temp link
temp->link = top;
// make temp as top of Stack
top = temp;
}
// Utility function to check if the stack is empty or not
int isEmpty()
{
return top == NULL;
}
// Utility function to return top element in a stack
int peek()
{
// check for empty stack
if (!isEmpty(top))
return top->data;
else
exit(EXIT_FAILURE);
}
// Utility function to pop top element from the stack
void pop()
{
struct Node* temp;
// check for stack underflow
if (top == NULL) {
printf("\nStack Underflow");
exit(1);
}
else {
// top assign into temp
temp = top;
// assign second node to top
top = top->link;
// destroy connection between first and second
temp->link = NULL;
// release memory of top node
free(temp);
}
}
void display() // remove at the beginning
{
struct Node* temp;
// check for stack underflow
if (top == NULL) {
printf("\nStack Underflow");
exit(1);
}
else {
temp = top;
while (temp != NULL) {
// print node data
printf("%d->", temp->data);
// assign temp link to temp
temp = temp->link;
}
}
}
int isFull()
{
return size>=CAPACITY;
}
// main function
int main()
{
int choice,x,data,y=1;
while(y==1)
{
printf("1. Push\n");
printf("2. Pop\n");
printf("3. isFull\n");
printf("4. isEmpty\n");
printf("5. Peek\n");
printf("6. Display\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter data to push into stack: ");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
x=isFull();
if(x==1)
{
printf("Stack is full");
}
else
{
printf("Stack is not full yet");
}
break;
case 4:
x=isEmpty();
if(x==1)
printf("Stack is empty");
else
{
printf("Stack is not empty");
}
break;
case 5:
x=peek();
printf("The topmost element is %d",x);
break;
case 6:
display();
break;
case 7:
printf("Exiting from app.\n");
exit(0);
break;
default:
printf("Invalid choice, please try again.\n");
}
printf("\n\n");
printf("Do you want to continue(1=Yes/0=N0: ");
scanf("%d",&y);
}
return 0;
}