You are on page 1of 9

16.

b.

#i
ncl
ude<st
dio.
h>

#def
ineSI
ZE20

i
ntar
ray[
SIZE]
;//decl
arat
ionofar
rayt
ypevar
iabl
e.

i
ntt
op1= -
1;

i
ntt
op2= SI
ZE;

/
/Funct
iont
opushdat
aint
ost
ack1

voi
dpush1(
intdat
a)

/
/checki
ngt
heover
flow condi
ti
on

i
f(t
op1< t
op2-1)

t
op1++;

ar
ray[
top1]= dat
a;

el
se

pr
int
f("
Stacki
sful
l")
;

/
/Funct
iont
opushdat
aint
ost
ack2

voi
dpush2(
intdat
a)

/
/checki
ngover
flow condi
ti
on
i
f(t
op1< t
op2-1)

t
op2-
-;

ar
ray[
top2]= dat
a;

el
se

pr
int
f("
Stacki
sful
l.
.\n"
);

/
/Funct
iont
opopdat
afr
om t
heSt
ack1

voi
dpop1(
)

/
/Checki
ngt
heunder
flow condi
ti
on

i
f(t
op1>= 0)

i
ntpopped_el
ement= ar
ray[
top1]
;

t
op1-
-;

pr
int
f("
%di
sbei
ngpoppedf
rom St
ack1\
n",popped_el
ement
);

el
se

pr
int
f("
Stacki
sEmpt
y\n"
);
}

/
/Funct
iont
oremovet
heel
ementf
rom t
heSt
ack2

voi
dpop2(
)

/
/Checki
ngunder
flow condi
ti
on

i
f(t
op2< SI
ZE)

i
ntpopped_el
ement= ar
ray[
top2]
;

t
op2-
-;

pr
int
f("
%di
sbei
ngpoppedf
rom St
ack1\
n",popped_el
ement
);

el
se

pr
int
f("
Stacki
sEmpt
y!\
n")
;

/
/Funct
ionst
oPr
intt
heval
uesofSt
ack1

voi
ddi
spl
ay_st
ack1(
)

i
nti
;

f
or(
i= t
op1;i>= 0;-
-i)

{
pr
int
f("
%d"
,ar
ray[
i])
;

pr
int
f("
\n"
);

/
/Funct
iont
opr
intt
heval
uesofSt
ack2

voi
ddi
spl
ay_st
ack2(
)

i
nti
;

f
or(
i= t
op2;i< SI
ZE;++i
)

pr
int
f("
%d"
,ar
ray[
i])
;

pr
int
f("
\n"
);

i
ntmai
n()

i
ntar
[SI
ZE]
;

i
nti
;

i
ntnum_of
_el
e;

pr
int
f("
Wecanpushat
otalof20val
ues\
n")
;

/
/Numberofel
ement
spushedi
nst
ack1i
s10

/
/Numberofel
ement
spushedi
nst
ack2i
s10
/
/loopt
oinser
ttheel
ement
sint
oSt
ack1

f
or(
i= 1;i<= 10;++i
)

push1(
i);

pr
int
f("
Val
uePushedi
nSt
ack1i
s%d\
n",i
);

/
/loopt
oinser
ttheel
ement
sint
oSt
ack2.

f
or(
i= 11;i<= 20;++i
)

push2(
i);

pr
int
f("
Val
uePushedi
nSt
ack2i
s%d\
n",i
);

/
/Pr
intBot
hSt
acks

di
spl
ay_st
ack1(
);

di
spl
ay_st
ack2(
);

/
/Pushi
ngonSt
ackFul
l

pr
int
f("
Pushi
ngVal
uei
nSt
ack1i
s%d\
n",11)
;

push1(
11)
;

/
/Poppi
ngAl
lEl
ement
sfr
om St
ack1

num_of
_el
e= t
op1+ 1;

whi
le(
num_of
_el
e)
{

pop1(
);

-
-num_of
_el
e;

/
/Tr
yingt
oPopt
heel
ementFr
om t
heEmpt
ySt
ack

pop1(
);

r
etur
n0;

Output
:

Wecanpushat
otalof20val
ues

Val
ue

Pushed

i
nSt
ack1i
s1

Val
uePushedi
nSt
ack1i
s2

Val
ue
Pushed

i
nSt
ack1i
s3

Val
uePushedi
nSt
ack1i
s4

Val
ue

Pushed

i
nSt
ack1i
s5

Val
uePushedi
nSt
ack1i
s6

Val
uePushedi
nSt
ack1i
s7

Val
uePushedi
nSt
ack1i
s8

Val
uePushedi
nSt
ack1i
s9

Val
uePushedi
nSt
ack1i
s10Val
uePushedi
nSt
ack2i
s11

Val
uePushedi
nSt
ack2i
s12

Val
uePushedi
nSt
ack2i
s13
Val
uePushedi
nSt
ack2i
s14Val
uePushedi
nSt
ack2i
s15

Val
uePushedi
nSt
ack2i
s16

Val
uePushedi
nSt
ack2i
s17

Val
uePushedi
nSt
ack2i
s18

Val
uePushedi
nSt
ack2i
s19

Val
uePushedi
nSt
ack2i
s20

10987654321

20191817161514131211

Pushi
ngVal
uei
nSt
ack1i
s11

St
ackFul
l1!CannotPush

10i
sbei
ngpoppedf
rom St
ack1

9i
sbei
ngpoppedf
rom St
ack18i
sbei
ngpoppedf
rom St
ack1
7i
sbei
ngpoppedf
rom St
ack16i
sbei
ngpoppedf
rom St
ack1

5i
sbei
ngpoppedf
rom St
ack1

4i
sbei
ngpoppedf
rom St
ack1

3i
sbei
ngpoppedf
rom St
ack1

2i
sbei
ngpoppedf
rom St
ack1

You might also like