STACKS & QUEUES
oe. ed ee
+ chapter ata Glance
tacks A stack is an abstract data type, which declares jacks.
. Cert either by an rege een ‘two methods PUSH and POP. Stac
‘The PUSH operation allows us to insert n
* The POP operation allows us to remove data from the end of en > jinked list
ASTACK is called a last in first out (LIFO) system ane? oF Ns!
stack may be represented by a linked list. The fist node of the list will be the topmost
clement of the stack and is pointed by a top pointer. This type of stack representation is called
linked stack. The stack can be declared as follows. eters
typedef struct linked_list
.
data atthe end of an array or linked lst.
int data;
struct Linked list’ next;
) Istack;
Istack *top;
«+ Various types of expression: A mathematical expression involves constants (operands) and
operations (operators). -
Infix notation: operand! operator operand2, A+B
Prefix jon: operator operand! operand2, + A B-
operand operand? operator, A B+
+ Conversion from INFIX to POSTFIX expression: In order to convert the infix to its
corresponding postfixes form; we need to do the following steps:
> Fully parenthesize the expression according to the priority of different operators,
> Move all operators so that they replace their corresponding right parentheses,
> Delete all parentheses.
‘The priorities of different operators are given below:
Operators Priority
Unary -, unary +, not (!) 4
*, 1, %, and (& / &&) 3
+, =, or (1/1) 2
aoe 1 ooo
© Priori 7 rity queue Is essentially a list of items in which each item has
Py Que & he A er pe ne
‘peak of one item having a higher priority than another. Given such a list we can determi
‘hich is the highest (or the lowest) priority item in the list. Items are inserted into a priority
4ueue in any, arbitrary order. However, items are withdrawn from a priority queue in order of
‘heir priorities. starting with the highest priority item first. Two ‘elements with the same
Priority are processed according {o the order in which they were added : the queue.
* Dequeue: De-queue is a linear data structure, where insertions and deletions are made to or
‘om ether end of the structure.
DSA-13
Scanned with CamScannerdded or removed eng 44, The ote
H oP
fst in wi et tack BUT 20mg 2), oP
2
4. Atineae is known 25 aoe cys yee out A
“jae neers ()
Asner) jon a*(b+¢)/e~fis keann
° infix express f 42, Stack cat
ression fore cyra+beef 4) Non a) evaluate
2.The Perea fix exer b) t+ aboef ) 2 eval
A wer) ¢) allocate
nswel a
ent mutual recursion is. d) conver
quired to implem' cyt “yt Reel)
3. The number of stacks re
a3 b)2
Answer: (C) 43. The followi
push(1), push(:
implemented using the sequence c
an be
Fn aan ca Mp) linked list ¢) heap She te
Answer: (d) Answer: (b)
5. Reserve Polish notation is often known as iw 14. The deque?
a) Infix by Prefix ¢) Postfix 4) to a) as a stac
Answer: (©) ©) both as 2
Answer: (b)
6. The evaluation of the postfix expression
3,5,7,4,4,12,% is . 415. Inserting an
a) 2. b)3 co qd): operation and d
operat
a) push, por
©) insert, de
Answer: (a)
Answer: (a)
7. The operation for adding an entry to a stack is traditionally called
tet : b) Append c) Insert
16. To make a qi
8. The best data str a) front = rez
- Structure to evaluate an arithmetic expression in postfix ang’) font = ai
a)Queue j nswer:
Answer: (b) 1) Stack ©) Tree 2
The heap (represen
Ite
80, 60.95 55 ie 4 by an array) constructed from the list of nu way Convert
, 0,10, 17,15 Ae yotack:
ant 2, 80-304 7,85, 15, 19 ) 80, 55, 60, 15/10, 30,12 wares >
) 4) none of these a Max
10. The posttix
r oquivatent of t
anne ioe : Veer Prefix * +ab—cit ig
‘ °) ab+cd* =
DSAL14
Scanned with CamScannerth ATA STRUL & ALGO!
44. The following sequence of
), POPs PUSH (1), push (2), py
gut values are
apatatlong fe befits te cn
OP, pop, poe 'S Performed on a’stack: push (1), push
* Push (2), pop. The sequence of popped
a)2,2,1,1,2 b)2,2,4,2, [WBUT 2014]
“Answer: (a) ae ©) 2,4,2,2.4 d) 2,4,2,2,2
42, Stack cannot be used to
a) evaluate an arithmeti, [WBUT 2015]
ic oe
b) implement recursion “P*°SS!0n in postfix form
¢) allocate resources (like CPU) by
t! ;
d) convert infix expression to ire caus g system
Answer: (¢) ix expression
8. Wee coer cf Fperatjons Is performed on a stack — [WBUT 2015]
ush(t), } POP( ), push(1), push
the sequence of popped out valuse sre POP POPC POPC), push(2), Pop),
2)2,2,4,2,1 -b)2,2,4,4,2 aya aed Per,
Answer: (b) a 1, 2,2,
14, The deque can be used 7 [WBUT 2016]
a)asa stack b) as a queue
¢) both as a stack and as a queue 4) none of these
Answer: (b)
15. Inserting an item into the stack when stack is not full is called
operation and deletion of item from the stack, when stack is not empty is called
.. operation. (WBUT 2017]
a) push, pop 'b) pop, push
c) insert, delete d) delete, insert
Answer: (a)
18. To make a queue empty, elements can be deleted till [wBuT 2018)
b) front = rear -1
d) None of these
ssion into equivalent postfix expression
a) front = rear + 4
¢) front = rear
Answer: (a)
1. a) Convert the following infix expre
Using stack: [WBUT 2009, 2010, 2015]
(4+ B)*C_ (DE) /(F +G)- [WBUT 2009]
5) What is a Max Heap?
DSA-15
Scanned with CamScannereames
rnewsanimseTe” (0-6) (E19)
poe: COON ope
K. to the heap we checl
2) —
key(B). This implies that an element with the great =
always in the root node, and so such a heap is sometimes called a max-heap. [Coe
2, What is a priority queue? LD ives ae
Mention the different desi ee
Pesce
amy, arb However, it (=
their priorities startin ems are with [LGke e:
Two elements withthe sane one Poort item First ENONE “2
the same pri
eue,
Were added to the que orty are processed according to the order i!
Pepa ot mole ira & heap is used to store the items in a pri
Tight cil. Each node (parent aa iefgs (08) and it has two children Ie
Property that the parent's value ig mee hes) has a value associated Wt
DSA-16 ler of its children.
Scanned with CamScannerto the heap we check to see
ia ese the child with the
anced,” ‘more or less than it. If it is more,
nan maybe swap again, unl the
They are
red list implementation:
rorted list implementation: Kee
tn Pali
ist of elements as the queue. To add an elément,
ia ito the end, To get the next
" na t elet
the highest priority. ‘Ment, search through al! elements for the one with
.efine dequeue?
What is dequeue? or, [WBUT 2010]
answer?
Dequeve is a linear data structure, where insertions and deletions are made to or from:
a st i
cher end of the structure. : acon ae ‘ :
[WBUT 2015]
4, Convert the following infix expressio
1 ec a ene ae eee” OY stone
1B +C*(D-E)—
4eBeC( )-F*G [WBUT 2041]
Answer
Symbol scanned ‘Output
A
A
AB
AB
AB'C
‘ABYC.
‘ABYC
AB*CD
ABYCD
‘ABYCDE
‘ABYCDE-
‘ABYCDE-*FG*-+
Scanned with CamScannerthe base address 2000, the,
ea
oy and
2] arin column major ordering,
ider the a
5, a) consider t
Scares of the array
m the beginning of th
Answer! ordering rar offset fro array tg
puted as:
(COLS + column
have address =
“Thus 9213) ou
il ory offset could then be computed as;
Incolumn major ordering een ofstumn*NUMROWS
3000 + 2+3*10 = 2032
“Thus af2][3) will have address =
reular queue over linear queue.
1) Wate the advantage of ot
Why circular queue is better ‘than sit
Why circular queue is used over simple queue
Answer: .
Refer to Question No. 5 of Long Answer Type Questions.
6, Evaluate the following postfix expression:
4,5,4,2, Ay tes 2,2, A190 3h
Write pseudo code for evaluating postfix expression.
Answet
1" Part
2” Part: :
art: Refer to Question No. 2 of Long Answer Type Questions.
7. How many t
Ho ypes of
Priority queue? If yes,
Answer: :
Mainly the ir
Mainly thee are wo kinds of
:
* priority queues are there? Can you consid
Priority queue: 1) Static priority queue 2) P
Stack can be consi
7 Considered as a priori
inserted are considered hi @ priority queue where the priorities of each ©
race fh ighe :
structure (ie, stack). Pet than the previous one. This will let it beh#¥®=
DSA-18
9. suppose 2 queue is im
& ment at the kth positio
‘answer? z
Arpeic int head, tai
of queue
Static char theQueue
) punction. =
7 Purpose
77 Returns
Initiali
void
[a7
Yoid InitQueue()
¢
head = tail -
)
ue
j/ Function: Enqueve
// Purpose: Enqueue
// Returns: TRUE if
us
or FALSE
int Enqueue(char ch,
c
int ¢;
// Cheek to
if (isFull())
// Increment
tail+
for (c
MAX!
array |e:
array [posit
return TRUE;
fa
1. Write ‘the differe
‘be differ
Priority queues
A p
Pees is a (ordered) col
sau nce and all deletions
. container of dé
jemPared to the order th
"tem that has been put las
Scanned with CamScanneraL IRE & ALGORITHM
4, Suppose @ qUCUE is implemented b,
element at the kth position of the array” Atay: Wr
an algorithm to insert a new
nswer ‘ ei
Marie int head,~tait yy, Decl. ee
F ai thas
of queue 8 global indices to head and tail
sratic Char theQueue(Max sre
// The queue
InitQueue()
Initialize
7/ Purpose
void
y/ Returns queue to empty,
I
yoid InitQueve()
(
head = tail = -
) :
Mee
y/ Function: Enqueue()
// Purpose: Enqueue an item into the kth positi
}/ Returns: TRUE if enqueue was successfus US
i or FALSE if the enqueue failed.
i
int Enqueue(char ch, k)
{
int ¢;
// Check to see if the Queue is full
if(isFull()) return FALSE;
// Increment tail ind
tail++; .
for (¢ = MAX_SIZE - 1; ¢ >= k - 1; c--)
array[c+1] = arraytcl:
array(position-1] = chi
) return TRUE; .
:
nd imploment the operations of
im
{. Write the difference between stack and queue and iP parapet
Priority queue. =
Answ
"Pay
A Stack is a (ordered) collection of
Sequence and all deletions always ATE TMT he gt data items out it oa order
is @ container of data items, [FO he container.
h ‘We can also say that the
jenheted to the order they have be Mere why a stack is also called LIFO
ing firs
"om that has been put last in is co” 2
psAi9
i | iisertions are made to the end of the
items, whe ‘end of the sequence. In principle a
Scanned with CamScannerJe item, whichis put fist ins
insertions are made to
ms, ae the beginning of thee.
are mata items are retrieved Out in the ke
hich - container that preserves the Order.
is put last into the queue is ty’
which s put Fst int the quae
rr
)
d
data getNext ()
supports the following three operat {
+4 parts that owing three data a1;
a al is jt data HYPE queue with an associated priority if (front.
APT Mee that has the highest priority. and prints
invent remove tM Sc iqssume structure is as belows) ua oust
using ar 4
7*C implementation : ai-ats
ceruct ta dealt
int val,p,07 ae t
ya(Wax :
; /*initial values*/ ;
int frontereare-ti /*4 aoe
- ;
+ /* inserthithPriority*/
void insert (data dl) swiss
t
Lf (rear==MAX-1) i
print€(*"Priority Queue is Full");
Write an algorithm to eval
else Answer:
aa + Scan the Infix string fro
ate * Initialise an empty stack
if (front==-1) * If the scannned charact
frontz0; character is an operator:
data temp; * If the scanned characte
for(int i=front;ic=rear;i++) . Precedence of the char
for(int jei+l;j, ear; j++)
topStack has higher pre
{ the scanned character t
topStack has precedence
Repeat this step till allt
(After all characters are
have to the Postfix stri
_ the stack. Repeat thi
'™m the Postfix string,
ria -P > a{j)-p)
1f(@L4] pesat slp)
if(a(il.o > afs)-2
DSA-20
Scanned with CamScannertemp=d[i];
ali}=a(31;
alj]=temp;
)
)
data getNext ()
(
data ai;
if (fronté=-1)
Printf(*Priority queue is Empty“);
front++;
return al;
}
2. Write an algorithm to convert an infix expression to postfix using stack.
[WBUT 2008, 2018]
OR, -
Write an algorithm to evaluate a postfix expression [WBUT 2017]
Answer:
Scan the Infix string from left to right.
Initialise an empty stack.
If the scannned character is an oper
character is an operator and if the stacl
If the scanned character is an are i
ter with the element
Ee err at onesie ice over the scanned character Pop the stack else Push
fnStack has higher pecedenes 0 ia as fg as Sack ot empty an
topStack has precedence over the character. a
Repeat his step il a Se He “rave to add any character that the stack may
ie io | ie nee Dave) stack isnot empty add ‘opSack to Postfix string and
Pop the stack, Repeat this step as long a stack is not empty.
Return the Postfix string.
rand, add it to the Postfix string. If the scanned
kis empty Push the character to stack.
rnd and the stack is not empty, compare the
‘on top of the stack (topStack). If
pSA-21
Scanned with CamScanneri wressiont 10
sion to convert a infis eX?"
The C-functi
i mo)
belo gctintonar *
‘ dot i = Aa)
while (x(i]
“se (isonphe txt}
princé(rees *C31);
else if (x{i] ==
: pret)
white (s{top] != "(70
inté(* %c*, popl :
pop? // delete '(' symbo’
)
else
nite (isp(sttop])>= icp (x{i]))
printé(* %c", pop()); push(x{i});
he
while (top > 0)
nile :
wMprinté(" tet, pop()):
)
)
int isp(char a)
: ”
if (aes
return (3);
else if (a =
return (2);
else if (a
return (1);
else if (a
heyy
retirn ( ~'1);
else if (a
return (0);
typ 8
int’ iep(char a)
€
if (ass te [ta
return (3)
‘else if (a
return (2);
else if (a
return (4)
else if (a
return (0);
DSA-22°
it’s equivatemt postfix ony, wa
a) Define cir
‘Answer:
Refer to Quest
b) Write an algo
‘Answer:
void OInsert,
“46 treat o:
(
print£(r9
return;
)
CQlt++rear) |
if (front
front = 0
)
c) What is input r
Answer:
An input-restricted
can only be made a
¢) Explain three us
Answer:
a) A Stack ADT is
end of the sequence
Principle a stack is a
order compared to th
the item that has bee
LIFO (Last In First,
Fontainer is get last o
>) Assume that the lis
typeder Struct n
qituct node
int data,
"Ptr nee,
“
function :
Bo Pop:
{ht Popinpes Peps
Scanned with CamScannerVe “A STRU ALGORITI
‘) Define circular queue.
‘answer? ut.
jas 10 Question No. scr (APUT S008, ae
& and
») Write an algorithm t orl ae desi fore Deaton
1m to inset
‘Answer? tan item in circular queue. [WBUT 2008, 2018]
foid Olnsert (int item)
(
if (rear
N-1)
printé("Queue Is Full");
return;
)
col++rear] = item;
if (front ail)
front =
)
c) What is input restricted Dequeue?
‘Answer: [WBUT 2008, 2018]
An input-restricted Dequeue is one where deletion can be made from both ends, but input
can only be made at one end.
a)What is a Stack ADT?
e a C function of popping an element from a stack implemented using linked
c) Explain three uses of stack data structure. : [WBUT 2009}
Answer:
2) A Stack ADT is a (ordered) collection of items, where all insertions are made to the
end of the sequence and all deletions always are made from the end of the sequence. In
principle a stack is a container of data items, from which we get data items out in reverse
order compared to the order they have been put into the container. We can also say that
the item that has been put last in is coming first out. That's why a stack is #s0 called
LIFO ((Last In First Out list). We can as well say that the item, which is put first in the
container is get last out (First In Last Out: FILO).
) Assume that the list is defined as below:
typedef struct node *nptr:
iruct node
int data;
ae next
I 5
function pop:*/
Te Popinper 8) /*Funetion +? POI
p the elements*/
DSA-23
Scanned with CamScannerAR PUBLICATIONS
wt yi
printertUnderflow on Pop’ 5 faced
repurn(-1)+ jy The two algor
or ‘ front pointer F n
7 Else ) amount of mem
Jos-onext->datai
temp=s->next
ge>next=temp->next;
free(temp) ;
return(y) ;
}
}
performing opera
certain intervals,
Let us consider
rear pointers.
fits
FR
©) Use of stack
evening Data: We edn use stacks toreverse data, =
(example: files, strings). It is very useful for finding palindrowes.
Consider the following pseudocode: ,
1) read (data)
2) loop (data not EOF and stac’
1) push (data)
2) read (data)
3) Loop (while stack notEmpty)
1) pop (data)
. 2) print (data)
Converting Decimal to Binary:
Consider the following pseudocode
Read (number)
Loop (number > 0)
1) digit = number! modulo 2
2) print (digit)
3) number = number / 2
The problem with this code is that it will prin i a
tthe binary ickward
becomes 11001000 instead of 00010011.) Pinay punto Oe
To remedy this problem, instead of printi git ri ;
stack. Then after the number is done bein je lat Hah ny, eS
and print it, eing converted, we pop the digit out
Evaluating arithmetic expressions.
rane level languages, infix notation cann
ue aston deci he
le is to convert a infix notation i :
done using stack, Rotation into postfix notation, then evaluati
% not full)
‘
To avoid this pre
prevents an excess
circular fashion w
circular queues,
So the above seq
queue).
ie
bal
i de
by SY oan see that
¥ Fedirecting the r
ae Snow writ
5. i) is ig. 2/80 assume:
') What is Circular queue? (2i8 comnseref:
fi) Write Q-insert algoris a
, gorithm f ir :
for the circular queue, = (fear
Write insert an elemen ue.
fe an algorithm to i lement into circu; :
lar queue.
DSA-24
Scanned with CamScannerA
vse DATA STRUCTURE & ALGORITHM
i The two algorithms QINSERT @ ¢
: DELE
Wie pointer F never memes TE can b i
anoant of Memory would be quit UP he rear pointer Acrea at uaage if he
‘ t ted to a : ee
forming operations on a Queue shausy eOmMOMat the elements The method of
Fertain intervals, nly be. used when the queue is emptied at
rear pointers.
tt
FR
insert
clip
Bic c
FR FR LPR
deleteA deleteB insert D insert E
. : overflow
To avoid this problem we can think an alternative representation of a queue, which
prevents an excessive use of memory. In this representation elements are arranged as in a
circular fashion where the rear again points to the front. This type of queue is known
circular queues.
So the above sequence of operations can be represented are shown below (in circular
queue).
A A
t
Ble le c]D]le
TH f Pit
ir insert E
deleteA delete B insert D Bt icy
ie fous case while inserting
As We can see that the overflow issue in the Prev tar queue.
Tediecting the rear to the front. This is the es
Fanetions of te circular queue implementation,
jelete functiélsd rear pointers respectively
ij os
) Letus now write the insert and d te
vei. also assume that F and R repre
(9 CQInsert (int item)
ct (rear == N-1)
Printé(*queue Is Full")/ psa-25
Scanned with CamScannert linked list data
(PORE struct Linked Tage Mucture. 5
int data;
DSA-26
SECTOR
POPULAR PUBLICATI
xeturn:
j = item
)
col++rear: ot
if (front
front
)
int copelete()
t
int x7
if (front == - 2)
printé("Queve Is Empty")?
exit (0);
x = coltront
col front]
if (front
front = rear =
else
frontt+;
return x;
)
In linear queue the condition for queue full is
QREAR=MAXLIMIT
Suppose. maxlimit is ten and queue is full now if we delete 9 element from qu
inspite of queue is empty we cannot insert any element in the queue, This
memory is solved through circular queue where queue full condition is
QREAR==Qfront+1
§ Define the ADT.for stack. Show the implementation of the stack data sin
ruc
using linked list. UT
Answer: . wel
The Stack ADT has two main functions:
Push
‘input: a stack data object
‘Output: indicatit
a Fee indicating whether the operation was successful.
'ent in the top position in the Sequence in the stack is removed.
Stack Implementation .
First, let us define the lint
struct 1
} Node;
For. us to be at
following code
// recursive
j/ of the st
void Display
. // recur
if (curr
€
retu
)
7/ the n
// disple
printé(*
// recurs
// aisple
Displayst
)
The code for pust
// push item
// this is sa
//-at the top
void Push(int
ce
// assump’
Program
7-1. ere:
Node. * tem
temp->date
/1 2. inse
temp->next
// 3. upda
head = tem
. 47 check £¢
cf (head =
Prince,
Yeturn
Scanned with CamScannerSS a
i nee a
struct linked_iis, RE & ALGORITHM
t *next;
) Node: it;
for. us to be able to test our code, we
following code uses recursion to displa mee 0 define a way to display our stack. The
j/ xecursively display ene my Stack
j/ of the stack ntents
wid DisplayStack(Node* currentnog, ;
le
:
// recursive terminatig, i
if (currentNode == nyzp) “ndition
{
return;
)
7/ the node is not null
// display the data .
print£(" -> @d", currentNode->data) ;
// recursively call the display to
// display the next element in the stack
DisplayStack(currentNode->next) ;
)
The code for pushing an element onto the stack is as given below.
// push item on the stack a
// this is same as adding a node :
//at the top of the list
void Push(int dataToadd)
( :
// assumption: héad is already defined elsewhere*in the
Program i, *
// 1,.ereate the new node
Node. *temp = new Node;
temp->data’ = dataToAdd;
// 2, insert it at the first position
temp->next = head;
; i se
// 3. update the head to point to this new no
) head '= temp:
oa
The code for popping an element from stack 8!
{| Pop an element from the stae!
i this is same as removing
/ from the list
(eet Pop()
n below.
/1 check for empty 14st
£ (head <= NULL)
. Ant):
printe ("stack is empty’
return NULL; psa-z7
Scanned with CamScanneree ae
POPULAR PUBLICATIONS
et the top nO
TeiestNode =
de
)
Wg head:
head
7/ disconnect tl
j7 €rom the 1ist
HrstNode->next = NULL
7/ return the top node
return firstNode;
)
7. What are th
insertion and del
Answer: .
1" Part: Refer to Question
2™ Part: Refer fo Question
8. a) Evaluate the postfix expres:
3,16, 2, #,°,12,6,
b) Convert the infix expressi
atbtc+(d*e+f)*g.
Answer:
a) The steps are as shown below:
3, 16,2, +, *, 12,6, y=
differences betwe
lotion in a circular Queue.
No. I(I" Part) of Long Answer Type Question, ’
‘No. 5 of Long Answer Type Questions, "
sion using stack:
jon Into its equivalent prefix expression using s
en stack and Queue? Write the agp,
n
p) First, the symbol
pushed onto the stac
Pefairs at this juncture
‘Stack
Next a *' is read. Th
nothing is output and
stack. Next, “c” is rea
‘Stack
The next symbol! is a '
the output, pop the ott
but equal priority, on
Input ‘Stack contents '
token OPeration (top on the right) Details =
3 Push on the stack 3 at
16 Push on the stack 3,16 The next symbol
2 Push onthe stack 3, 16,2 : Of tte stack, Then "di
+ Add 3,18 Pop two values: 16 and 2 and post
result 18 on the stack
* Multiply 54 Pop two values: 3 and 18 and c
12. Pushonthestack 4g, “Sut S4onthestack ———
6 Push on the stack 54, 12,6 We i
, wn cotinue by rea
e cal na closed
1 Divide 54.2 Pop two values: 12 and 6 #4PBsy Oru, Pave
. a result 2 on the stack .
— Divide . 54,2 Pop two values: 54 and 2 ad t+ -
f . g
(icet (Return th rest 3 result 52 on the stack on =
: : Pop the only value 52 and 0 Le]
- = Stack
DSA28
Scanned with CamScannerDATA STRUCTURE & ALGORITHM
first, the symbol “a” is read, so it is
Pshed ono the stack, Next “bg read ange tTeush to the output. Then "is read and
Pris at this juncture is as follows: Passed through to the output. The state of
ab
Stack
Next a*' is read. The top entry on th + Output
rothing is output and" is put on the io aes ace fas lower precedence thatl:7,i59
stack. Next, “c” is read and output, Thus far, we have
- : abe
‘Stack Cupar
The next symbol is a '+'. Checking the stack, we find that we will pop a" and place it on
the output, pop the other '+', which is not of lower
but equal priority, on the stack, and then push the ‘+.
ebort
+
‘Output
The next symbol read is an ‘(, which, being of highest precedence is placed on
the stack. Then ‘“d” ig read and output.
¢
abertd
‘Output
not” get removed except
“e” is read and
Stack
Wi ‘Stack
© continue by reading @
‘When a closed parenthesis is being proe
Output,
c ie el a
4 Output :
+
11 parentheses do
we Sice open Paes Net
ess!
Stack pSA-29
Scanned with CamScannerpuBLicAtio!
id output ‘*” and then push «1
jsavt. We pop am
“The next symbol read is 8 ** |
-—] [Symbol seann
and output. :
: [Lesa cera
3 p2———___
[Zea Punk cree
[pei es
ze
0 : —
Siek .
is emptied bac] a
Now we read a 7 =
[NONE
NONE
date Beng
abevtderte
Ontput
‘Stack
We read a'*' next; it is pushed onto the stack. Then “g” is read and output,
a abertdes tte
+ Stele ~ Ounpun
The input is now empty, so we pop and output symbols from the stack until it
abertdes tiger
= Output
8. Convert the infix expression 9+5*7-642415/3 into its. equival
stack. that postfix expression, clearly showing the
‘Symbol scanned
*Jalsfo
|
Scanned with CamScanner— ‘TA STRI IRE & Al
[—Sumibel scanned Stack Output
7 a
957%
— ; bs
: 7 95746
: = 9577462.
ice 7 S57 62A-
i = 95756215
; 21 95746215
2 a 957462153
NONE: NONE IST EIS3/e
Scanned with CamScanner