-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_using_array.c
69 lines (69 loc) · 1.17 KB
/
Stack_using_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
/*Write a menu driven C program to implement stack using one
dimensional array*/
#include<stdio.h>
int t=-1,n=10,st[10];
void push();
void pop();
void peek();
void display();
int main()
{ int ch;
while(1)
{ printf("\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n");
printf("Enter Choice:");
scanf("%d",&ch);
switch(ch)
{ case 1: push();
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: display();
break;
case 5: return 0;
}
}
return 0;
}
void push()
{ int d;
if(t==n-1)
{printf("Stack Overflow!!\n");
}
else
{ printf("Enter element to be pushed:");
scanf("%d",&d);
t=t+1;
st[t]=d;
}
}
void pop()
{ if(t==-1)
{printf("Stack is empty!!\n");
}
else
{ printf("Popped element=%d\n",st[t]);
t=t-1;
}
}
void peek()
{ if(t==-1)
{ printf("Stack is empty!!\n");
}
else
{ printf("Top Element=%d",st[t]);
}
}
void display()
{ int i;
if(t==-1)
{printf("Stack is empty!!\n");
}
else
{ printf("Stack=\n");
for(i=t;i>=0;i--)
{printf("%d\n",st[i]);
}
}
}