-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_array.c
84 lines (78 loc) · 1.31 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
#include<stdio.h>
#include <stdlib.h>
int i,n,ele,choice,stack[50],top=-1;
void display();
void menu();
void peek();
void push();
void pop();
void main()
{
printf("Enter the stack size:");
scanf("%d",&n);
menu();
}
void menu()
{
printf("\n Menu\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:push(ele);
break;
case 2:pop();
break;
case 3:peek();
break;
case 4:display();
break;
case 5:exit(1);
default:printf("Enter a valid choice");
menu();
}
}
void push()
{
printf("Enter the element:");
scanf("%d",&ele);
if(top!=n-1)
{
top=top+1;
stack[top]=ele;
printf("Insertion successfull");
}
else
printf("cant insert stack full");
menu();
}
void peek()
{
printf("The top most element of the stack is :%d",stack[top]);
menu();
}
void pop()
{
if(top==-1)
printf("coudnt retrieve,stack empty\n");
else
{
ele=stack[top];
printf("Deleted: %d",ele);
top=top-1;
}
menu();
}
void display()
{
i=0;
if(top==-1)
printf("stack is empty");
else
{
printf("The stack elements are:");
for(i=top;i>=0;--i)
printf("\t%d",stack[i]);
}
menu();
}