-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stack_Operations.c
102 lines (94 loc) · 1.87 KB
/
Stack_Operations.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
/**********************************************************************************
* File : Stack_Operations.c
* Program : Program to perform stack operations
* Language : C
* Author : Tom Sibu
* Version : 1.0
* Date : 18/10/2023
***********************************************************************************/
#include <stdio.h>
int top=-1;
void push(int[],int);
void pop(int[]);
void display(int[]);
void peek(int[]);
int main() {
int max,operation;
printf("Enter the max size of stack : ");
scanf("%d",&max);
int stack[max];
do {
printf("Enter the operation to be performed :\n1. Push\n2. Pop\n3. Display\n4. Peek\n5. Exit\nChoice : ");
scanf("%d",&operation);
switch (operation) {
case 1: {
push(stack,max);
break;
}
case 2: {
pop(stack);
break;
}
case 3: {
display(stack);
break;
}
case 4: {
peek(stack);
break;
}
case 5: {
printf("Exiting the program.\n");
return 0;
}
default: {
printf("Invalid operation : Try again\n");
break;
}
}
} while (operation!=5);
return 0;
}
void push(int stack[],int max) {
int element;
if (top==max-1) {
printf("Stack Overflow condition : Stack is full\n");
}
else {
printf("Enter the element : ");
scanf("%d",&element);
top++;
stack[top]=element;
printf("Pushed : %d\n",element);
}
}
void pop(int stack[]) {
int element;
if (top==-1) {
printf("Stack Underflow condition : Stack is empty\n");
}
else {
element=stack[top];
top--;
printf("Popped : %d\n",element);
}
}
void display(int stack[]) {
int i;
printf("Stack : ");
if (top==-1) {
printf("Empty");
}
for (i=top;i>=0;i--) {
printf("%d ",stack[i]);
}
printf("\n");
}
void peek(int stack []) {
if (top==-1) {
printf("Stack is Empty");
}
else {
printf("Topmost element : %d\n",stack[top]);
}
}