forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackImplementationWithArray.c
161 lines (150 loc) · 2.67 KB
/
StackImplementationWithArray.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
/*
A stack is an Abstract Data Type (ADT), commonly used in most programming languages.This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first.
The Basic operation in Stack are:
1.push() − Pushing (storing) an element on the stack.
2.pop() − Removing (accessing) an element from the stack.
3.peek() − get the top data element of the stack, without removing it.
4.display() - used to display the elements
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define SIZE 10
void push(int);
void pop();
void peek();
void display();
int stack[SIZE], top = -1;
void main()
{
int s;
printf("Enter the array size: ");
scanf("%d", &s);
int a[s];
int value, choice;
while (1)
{
printf("\n\n*****MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Peek\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
exit(0);
default:
printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value)
{
if (top == SIZE - 1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else
{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop()
{
if (top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else
{
printf("\nDeleted Element : %d", stack[top]);
top--;
}
}
void display()
{
if (top == -1)
printf("\nStack is Empty!!!");
else
{
int i;
printf("\nStack elements are:\n");
for (i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
}
void peek()
{
if (top == -1)
printf("\nstack is empty!!!");
else
{
printf("\nTop element of stack is %d", stack[top]);
}
}
/*
Enter the array size: 10
***** MENU *****
1. Push
2. Pop
3. Display
4. peek
5. exit
Enter your choice: 1
Enter the value to be insert: 45
Insertion Success!!!
***** MENU *****
1. Push
2. Pop
3. Display
4. peek
5. exit
Enter your choice: 1
Enter the value to be insert: 20
Insertion Success!!!
***** MENU *****
1. Push
2. Pop
3. Display
4. peek
5. exit
Enter your choice: 3
Stack elements are: 45 20
***** MENU *****
1. Push
2. Pop
3. Display
4. peek
5. exit
Enter your choice: 4
Top element of Stack is: 20
***** MENU *****
1. Push
2. Pop
3. Display
4. peek
5. exit
Enter your choice: 2
Deleted Element : 20
***** MENU *****
1. Push
2. Pop
3. Display
4. peek
5. exit
Enter your choice: 3
Stack elements are: 45
Time complexity : O(1)
Space Complexity: O(n)
*/