-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_stack.c
More file actions
100 lines (98 loc) · 1.44 KB
/
reverse_stack.c
File metadata and controls
100 lines (98 loc) · 1.44 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<stdio.h>
#include<stdlib.h>
#define MAX 15
int a[MAX],top;
int b[MAX];
void push(int x)
{
if(top==MAX-1)
{
printf("Stack is Full. Can't Push\n");
}
else
{
top++;
a[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("The Stack is Empty. Can't pop\n");
}
else
{
printf("The popped element is %d",a[top]);
top--;
}
}
void reverse() {
int j = 0,i;
for(i = top; i >= 0; i--) {
b[j++] = a[i];
}
printf("Reversed Stack: ");
for(i = 0; i < j; i++) {
printf("%d ", b[i]);
}
printf("\n");
}
void display()
{
int i;
if(top==-1)
{
printf("Empty Stack. Nothing to display");
}
else
{
printf("The elements of the Stack are ");
for(i=0;i<=top;i++)
printf("%d ",a[i]);
}
}
void menu()
{
printf("\n");
printf("\t\t\t PROGRAM ON Stack\n");
printf("\t\t\t 1. PUSH\n");
printf("\t\t\t 2. POP\n");
printf("\t\t\t 3. DISPLAY\n");
printf("\t\t\t 4. REVERSE\n");
printf("\t\t\t 5. EXIT\n");
printf("Enter your choice ");
}
int main()
{
int ch,m;
top=-1;
while(875)
{
menu();
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the number to push ");
scanf("%d",&m);
push(m);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
reverse();
break;
case 5:
exit(0);
default:
printf("\nInvalid choice \n");
break;
}
}
return 0;
}