-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
87 lines (79 loc) · 1.51 KB
/
stack.cpp
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
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
int st[30],n,top=-1,i,x;
bool isFull()
{
if(top==n-1)
return true;
else
return false;
}
bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}
void push(int x)
{
if(isFull())
printf("Stack is full\n");
else
{
top++;
st[top]=x;
}
}
void pop()
{
if(isEmpty())
printf("Stack is empty\n");
else
{
printf("Deleted element is %d\n",st[top]);
top--;
}
}
void display()
{
if(isEmpty())
printf("Stack is empty\n");
else
{
printf("Stack elements are:\n");
for(i=top;i>=0;i--)
printf("%d\n",st[i]);
}
}
int peek() {
if(isEmpty())
printf("Stack is empty\n");
else
return st[top];
}
void main() {
int ch;
printf("Enter the size of stack\n");
scanf("%d",&n);
while(1) {
printf("1.Push\n2.Pop\n3.Display\n4.Peek\n5.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch) {
case 1: printf("Enter the element to be inserted\n");
scanf("%d",&x);
push(x);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("Top element is %d\n",peek());
break;
case 5: exit(0);
default: printf("Invalid choice\n");
}
}
}