-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStack_using_Array.c
More file actions
71 lines (71 loc) · 1.29 KB
/
Stack_using_Array.c
File metadata and controls
71 lines (71 loc) · 1.29 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
#include<stdio.h>
#include<stdlib.h>
#define max 50
int stack[max],n,x,op,top,i;
void push(void);
void pop(void);
void peek(void);
void display(void);
int main()
{
top=-1;
printf("Enter size of the stack : ");
scanf("%d",&n);
do
{
printf("Enter your choice : \n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
scanf("%d",&op);
switch(op)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : peek();
break;
case 4 : display();
break;
case 5 : exit(0);
default : printf("Wrong Choice !!\n");
}
}
while(op!=0);
}
void push()
{
if(top==n-1)
printf("Stack is full !!\n");
else
{
top++;
printf("Enter data : ");
scanf("%d",&x);
stack[top]=x;
}
}
void pop()
{
int y;
if(top==-1)
printf("Stack is empty !!\n");
else
{
y=stack[top];
top--;
printf("The popped element is %d\n",y);
}
}
void peek()
{
if(top==-1)
printf("Stack is empty !!\n");
else
{
printf("The topmost element is %d\n",stack[top]);
}
}
void display()
{
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}