-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_array.c
100 lines (94 loc) · 1.73 KB
/
stack_array.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
#include <stdio.h>
#include<stdlib.h>
#define max 50
struct stack
{
int a[max];
int top;
};
void initialize(struct stack *s)
{
s->top = -1;
}
void push(struct stack *s, int element)
{
if(s->top == (max-1))
{
printf("Stack Overflow");
return;
}
s->top++;
s->a[s->top] = element;
}
int pop(struct stack *s)
{
if(s->top == -1)
{
printf("Stack Underflow");
return -1;
}
int x=s->a[s->top];
s->top--;
return x;
}
int peep(struct stack *s)
{
if(s->top == -1)
{
printf("Stack Empty");
return -1;
}
int x=s->a[s->top];
return x;
}
int main()
{
struct stack *s=(struct stack*)malloc(sizeof(struct stack));
initialize(s);
int choice;
while(1)
{
printf("\n1. Insert element in stack (push)\n");
printf("2. Delete element in stack (pop)\n");
printf("3. Display element in stack (peep)\n");
printf("4. Exit\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
{
int element;
printf("Enter element to insert in stack\n");
scanf("%d",&element);
push(s, element);
break;
}
case 2:
{
int delete;
delete = pop(s);
printf("%d deleted from stack\n",delete);
break;
}
case 3:
{
int topmost;
topmost = peep(s);
printf("topmost element in stack = %d\n",topmost);
break;
}
case 4:
{
printf("Exiting...\n");
return 0;
}
default:
{
printf("Invalid Input!\n");
break;
}
}
}
return 0;
}