MENU

Chapter 9 Stack Solutions

Question - 1 : -
Evaluate the following postfix expression. Show the status of stack after execution of each operation separately:
2,13, + , 5, -,6,3,/,5,*,<

Answer - 1 : -

ITEM
SCANNED

OPERATION

STACK

2

PUSH 2

2

13

PUSH 13

2,13

+

POP 13 and 2
Evaluate 2 + 13 = 15
PUSH 15

15

5

PUSH 5

15,5

POP 5 & 15
EVALUATE 15-5 = 10
PUSH 10

10

6

PUSH 6

10, 6

3

PUSH 3

10, 6, 3

/

POP 3 & 6
EVALUATE 6/3= 2
PUSH 2

10,2

5

PUSH 5

10, 2, 5

*

POP 5 & 2
EVALUATE 2*5 = 10
PUSH 10

10, 10

< 

POP 10 & 10
EVALUATE
10<10 = FALSE
PUSH FALSE

FALSE

RESULT = FALSE

Question - 2 : -
Evaluate the following postfix expression : (show status of Stack after each operation)
100,40,8,/,20,10,-,+,*

Answer - 2 : -

ITEM

SCANNED

OPERATION

STACK

100

PUSH 100

100

40

PUSH 40

100,40

8

PUSH 8

100,40,8

/

POP 8
POP 40
EVALUATE 40/8 =5
PUSH 5

100,5

20

PUSH 20

100,5,20

10

PUSH 10

100, 5, 20, 10

POP 10
POP 20
EVALUATE 20-10 =10
PUSH 10

100,5,10

+

POP 10 POP 5
EVALUATE 10 + 5= 15 PUSH 15

100,15

*

POP 15
POP 100
EVALUATE 100 * 15 = 1500
PUSH 1500

1500

Question - 3 : -
Evaluate the following postfix expression. Show the status of stack after execution of each operation separately:
F, T, NOT, AND, F, OR, T, AND

Answer - 3 : -

S.No.

Scanned Element

Operation

Stack

1

F

PUSH F

F

2

T

PUSH T

F,T

3

NOT

Calculate NOT T

POP T

PUSH F

F

F,F

4

AND

Calculate NOT

POP F

POP F

PUSH F

F

F

5

F

PUSH F

F,F

6

OR

POP F

POP F

 

7

T

PUSH T

F,T

8

AND

POP T

POP F

PUSH F

F

F

Thus the stack willhave False Value

Question - 4 : -
Evaluate the following postfix expression using a stack and show the contents of stack after execution of each operation:
5,3,2, *, 4,2, /, -,*

Answer - 4 : -

SYMBOL

STACK

OUTPUT

5

5

3

5,3

2

5,3,2

*

PUSH 3,2

Perform 3*2=6

POP 6

5

5

5,6

4

5,6,4

2

5,6,4,2

/

PUSH 4,2

Perform 4/2=2

POP2

5,6

5,6

5,6,2

PUSH 6,2

Perform 6-2=4

POP 4

5

5

5,4

*

PUSH 5,4

perform 5*4=20.

POP 20

20

Result=20

Question - 5 : -
Evaluate the following POSTFIX notation. Show status of Stack after every step of evaluation (i.e. after each operation)
False NOT, True, AND, True, False, OR, AND

Answer - 5 : -

Element Scanned

Stack Status

False

False

NOT

True

True

True, True

AND

True

True

True, True

False

True, True, False

OR

True, True

AND

True

Final Answer: True

Question - 6 : -
Top is a pointer variable pointing to the top element of a stack, with each node having the following structure declaration:
struct Stack {int Data, Stack * Next};
Considering the above explanation, what will the following code do ?
int count = 0, Sum = 0;

Stack * Temp = Top;
 while (Temp - > Next! = NULL)
 { count + +;
 Sum + = Temp - > Data;
 Temp Temp - > Next;
 }
 count < < Sum / count;

Answer - 6 : -

It will calculate the average of stack values.

Question - 7 : -
Convert the expression ((x * 3 + y * 3 + z * 3) / (x + y + z)) into postfix expression. Show the content of the stack during the conversion.

Answer - 7 : -

Given expression : ((x * 3 + y * 3 + z * 3) / (x + y + z))
((x * 3 + y * 3 + z * 3) / (x + y + z))

Symbol

Scanned

Stack

Expression

(

(

X

*

(

((

((

((*

X

X

3

+

y

X-

3

+

z

X-

3

)

/

(

X

+

y

+

Z

)

)

((*

((* +

((* +

((* + *

((* + *

((* + * +

((* + * +

((* + * + *

((* + * + *

(

(/

(/(

(/(

(/(+

(/(+

(/(+ +

(/

x3

x3

x3y

x3y

x3y3

x3y3

x3y3z

x3y3z

x3y3z3

x3y3z3 * + * + *

x3y3z3 * + * + *

x3y3z3* + * + *

x3y3z3 * + * + * x

x3y3z3 * + * + * x       •

x3y3z3 * + * + * x y

x3y3z3 * + * + * x y

x3y3z3 * + * + * xyz

x3y3z3 * + * + * xyz + +

x3y3z3 * + * + * xyz + +/

\Postfix expression is: x3y3z3 * + * + * xyz ++/

 

Question - 8 : -
Evaluate the following POSTFIX expression, show the status of Stack after execution of each operation separaterly:
45,45,+,32,20,10,/,-,*

Answer - 8 : -

Element Scanned

Stack Status

45

45

 45

 45,45

+

 90

 32

90,32

20

90,32,20

 10

90,32,20,10

/

90,32,2

90,30

*

2700

Hence the final resultis 2700

Question - 9 : -
Write the definition of a member function Pop ()
in C++, to delete a book from a dynamic stack of TEXTBOOKS considering the following code is already included in the program.

Struct TEXTBOOKS
 {
 Char ISBN [20]; Char TITLE [80]; TEXTBOOKS *Link;
 };
 class STACK
 {
 TEXTBOOKS *Top; 

public :
 STACK () {Top = NULL;} 

void Push ();
 . void pop );
 -STACK ();
 };

Answer - 9 : -

void STACK : : POP ()
 {
 if (Top ! = NULL)
{
 TEXTBOOKS *Temp;
 Temp=Top;
cout<< TOP- >ISBN<
 TITLE<<"delected"<
 Top=Top-Link;
 delete Temp;
 }
 else
 cout<<"Stack Empty"<
 }
OR
Any other correct equivalent function definition

Question - 10 : -
Write the defintion of a member function PUSH () in C+ +, to add a new book in a dynamic stack of BOOKS considering the following code is already included in the program :

struct BOOKS
 {
 Char ISBN [20]; TITLE[80];
 BOOKS *Link;
 };
 class STACK
 {
 BOOKS *Top;
 public :
 STACK () {Top = NULL;}
 void PUSH ();
 Void POP ();
 -STACK ();
 };

Answer - 10 : -

void STACK :: PUSH ()
 {
 BOOKS *Temp;
 Temp=New BOOKS;
 gets (Temp->ISBN);
 gets (Temp->TITLE);
 Temp->Link =Top;
 Top=Temp;
 }
OR
Any other correct equivalent function definition

×