-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.c
More file actions
99 lines (88 loc) · 1.77 KB
/
StackArray.c
File metadata and controls
99 lines (88 loc) · 1.77 KB
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
// C Program to perform stack implementation
#include<stdio.h>
int isFull();
int isEmpty();
void push(int );
void pop();
void peek();
struct stackk
{
int stk[10];
int top;
} s;
void main()
{
s.top = -1;
int ch;
int loop = 1;
int new;
int i;
do
{
printf("\n ***STACK OPERATIONS");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n ***************");
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter item to be pushed: ");
scanf("%d", &new);
push(new);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
for(i=s.top;i>-1;i--)
{
printf("\t%d\n", s.stk[i]);
}
break;
case 5:
printf("Termination Successful!\n");
loop = 0;
break;
default:
printf("Invalid choice\n");
}
} while(loop);
}
int isFull()
{
if(s.top >= 9) return 1;
else return 0;
}
int isEmpty()
{
if(s.top <= -1) return 1;
else return 0;
}
void push(int new)
{
if(isFull()) printf("Stack Overflow!\n");
else {
//s.top ++;
s.stk[++s.top] = new;
}
}
void pop()
{
if(isEmpty()) printf("Stack Underflow!\n");
else {
printf("Item popped is: %d\n", s.stk[s.top--]);
// s.top --;
}
}
void peek()
{
printf("%d is on top of the stack.\n", s.stk[s.top]);
}