forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacks.c
163 lines (152 loc) · 3.09 KB
/
Stacks.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
// C program to implement stack using linked list
#include<stdio.h>
struct stack
{
int top;
int data[100];
} Stack;
int count = 0;
// Function to push element to stack
void push(struct stack *s, int value, int n)
{
if (s -> top == n-1)
{
printf("\nStack is full");
}
else
{
s -> top++;
s -> data[s -> top] = value;
}
count++;
}
// Function to pop element from stack
void pop(struct stack *s)
{
if (s -> top == -1)
printf("\nThe stack is empty");
else
{
int value;
value = s -> data[s -> top];
printf("\nThe poped value is %d", value);
s -> top--;
}
count--;
}
// function to check if the stack is empty or not
int isEmpty(struct stack *s)
{
return s -> top == NULL;
}
// function to return top element in a stack
int peek(struct stack *s)
{
//check for empty stack
if (!isEmpty(s -> top))
return s -> data[s -> top];
else
exit(1);
}
// Function to print stack
void printStack(struct stack *s)
{
if (Stack.top == -1)
{
printf("\nStack is empty");
}
else
{
printf("\nThe current stack is\n");
for (int i = 0; i <= Stack.top; i++)
printf("%d\t", s -> data[i]);
}
}
// Function to print size of stack
void size(struct stack *s)
{
printf("\nSize of stack is %d", count);
}
// Main function
void main()
{
struct stack *s;
s = &Stack;
Stack.top = -1;
int c, value, k, n;
printf("\nEnter the number of elements");
scanf("%d", &n);
do
{
printf("\nWhich operation to perform \n 1.Push \n 2.Pop \n 3.Check empty or not");
printf("\n 4.Print top element \n 5.Display stack \n 6.Size"); // Menu Driven Programming
printf("\nEnter choice");
scanf("%d", &c);
if (c == 1)
{
printf("\nEnter value to push ");
scanf("%d", &value);
push(s, value, n);
}
else if (c == 2)
{
pop(s);
}
else if (c == 3)
{
if (!isEmpty(s))
printf("\nStack is not empty");
else
printf("\nStack is empty");
}
else if (c == 4)
{
printf("\nThe topmost element is ");
peek(s);
}
else if (c == 5)
{
printStack(s);
}
else if (c == 6)
{
size(s);
}
printf("\nDo u want to continue(1/0)");
scanf("%d", &k);
} while (k != 0);
printf("\n");
}
/*
Sample output:
Enter the number of elements 4
Which operation to perform
1.Push
2.Pop
3.Check empty or not
4.Print top element
5.Display stack
6.Size
Enter choice 1
Enter the value to push 3
Do u want to continue(1/0) 1
Which operation to perform
1.Push
2.Pop
3.Check empty or not
4.Print top element
5.Display stack
6.Size
Enter choice 2
The popped value is 3
Do u want to continue(1/0) 1
Which operation to perform
1.Push
2.Pop
3.Check empty or not
4.Print top element
5.Display stack
6.Size
Enter choice 5
Stack is empty
*/