forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
80 lines (80 loc) · 1.37 KB
/
stack.cpp
File metadata and controls
80 lines (80 loc) · 1.37 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
//
// A Stack is an abstract data type that serves as a collection
// of elements, with two principal operations are push & pop
//
// The All ▲lgorithms Project
//
// https://allalgorithms.com/data-scructures/
// https://github.com/allalgorithms/cpp
//
// Contributed by: ANUJ MODI
// Github: @descifrado
//
#include<stdio.h>
#define max 50
struct stack
{
int ch[max];
int top;
};
typedef struct stack Stack;
void push(Stack *s, int x)
{
if(s->top==max-1)
printf("ERROR: Stack Overflow\n");
else
s->ch[++s->top]=x;
}
int pop(Stack *s)
{
if(s->top==-1)
{
printf("ERROR: Stack Underflow\n");
}
else
return (s->ch[s->top--]);
}
int main()
{
printf("Stack Has Been Initiated...\n");
Stack s;
s.top=-1;
int c;
while(1)
{
printf("\nEnter 1 to push an element\nEnter 2 to pop an element\nEnter 3 to display all the content of stack\nEnter 0 to exit\n");
scanf("%d",&c);
switch(c)
{
case 0: return 0;
case 1:
{
printf("Enter element to be pushed\n");
int x;
scanf("%d",&x);
push(&s,x);
printf("Element successfully pushed\n");
break;
}
case 2:
{
printf("Poped Element Is %d\n",pop(&s));
break;
}
case 3:
{
printf("Stack has the following content in order LIFO\n");
int i;
for(i=s.top;i>=0;i--)
{
printf("%d ",s.ch[i]);
}
break;
}
default:
{
printf("Wrong Choice\n");
}
}
}
}