-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_list.c
180 lines (160 loc) · 3.42 KB
/
stack_list.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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Stack
{
struct Node* top;
};
struct Stack* CREATE()
{
struct Stack* S = (struct Stack*)malloc(sizeof(struct Stack));
S->top = NULL;
return S;
}
int IsEmpty(struct Stack* S)
{
if (S->top == NULL)
{
return 1;
}
return 0;
}
int IsFull(struct Stack* S)
{
return 0;
}
void PUSH(struct Stack* S, int x)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("Memory allocation failed. Stack overflow.\n");
return;
}
newNode->data = x;
newNode->next = S->top;
S->top = newNode;
printf("Pushed %d onto the stack.\n", x);
}
void POP(struct Stack* S)
{
int x=IsEmpty(S);
if (x==1)
{
printf("Stack empty. \n");
return;
}
struct Node* temp = S->top;
int poppedValue = temp->data;
S->top = S->top->next;
free(temp);
printf("Popped %d from the stack.\n", poppedValue);
}
int Top(struct Stack* S)
{
int x=IsEmpty(S);
if (x==1)
{
printf("Stack is empty. \n");
return -1;
}
return S->top->data;
}
void Display(struct Stack* S)
{
int x=IsEmpty(S);
if (x==1)
{
printf("Stack is empty.\n");
return;
}
struct Node* temp = S->top;
printf("Stack contents (top to bottom): ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
struct Stack* S = CREATE();
while(1)
{
int ch;
printf("1.Insert an element at the top of stack\n");
printf("2.Delete the top element of the stack\n");
printf("3.Check if Stack is full\n");
printf("4.Check if Stack is empty\n");
printf("5.Display the top element of the stack\n");
printf("6.Display the elements of the stack\n");
printf("7.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
{
int ele;
printf("Enter element to insert\n");
scanf("%d",&ele);
PUSH(S,ele);
break;
}
case 2:
{
printf("Top element: %d\n", Top(S));
POP(S);
break;
}
case 3:
{
int x=IsFull(S);
if(x==0)
{
printf("stack is not full \n");
break;
}
printf("stack is full \n");
break;
}
case 4:
{
int x=IsEmpty(S);
if(x==1)
{
printf("stack is empty \n");
break;
}
printf("stack is not empty \n");
break;
}
case 5:
{
printf("Top element: %d\n", Top(S));
break;
}
case 6:
{
Display(S);
break;
}
case 7:
{
printf("Exiting...\n");
return 0;
}
default:
{
printf("Invalid Input!\n");
break;
}
}
}
return 0;
}