-
Notifications
You must be signed in to change notification settings - Fork 13
/
stack.cpp
103 lines (102 loc) · 1.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
int size;
int topb=-1,topf=100,stack[100];
void pushb(int k)
{
stack[topb+1]=k;
topb+=1;
}
void pushf(int k)
{
stack[topf-1]=k;
topf-=1;
}
void popb()
{
topb-=1;
}
void popf()
{
topf+=1;
}
int main()
{
int f=1;
int a,b,loop=1;
printf("enter no of pages\n");
scanf("%d",&b);
while(loop)
{
printf("current page is %d\n",f);
printf("1:moving forward\n");
printf("2:moving backward\n");
printf("3:printing forward stack\n");
printf("4:printing backward stack\n");
printf("5:end\n");
printf("enter a choice\n");
scanf("%d",&a);
if(a==1)
{
if(f==b)
printf("no more forward pages\n");
else
{
pushb(f);
if(stack[topf]==f+1)
{
popf();
}
f+=1;
}
}
else if(a==2)
{
if(f==1)
printf("no backward pages\n");
else
{
pushf(f);
popb();
f-=1;
}
}
else if(a==3)
{
int i;
if(topf==100)
{
printf("forward array is empty\n");
}
else
{
printf("forward array is\n\n");
for(i=topf;i<100;i++)
{
printf("%d\n",stack[i]);
}
printf("\n\n");
}
}
else if(a==4)
{
if (topb==-1)
{
printf("backward array is empty\n");
}
else
{
int i;
printf("backward array is\n\n");
for(i=topb;i>=0;i--)
{
printf("%d\n",stack[i]);
}
printf("\n\n");
}
}
else if(a==5)
{
loop=0;
}
}
}