MENU

Chapter 9 Stack Solutions

Question - 11 : -
Convert the following Infix expression to its equivalent Postfix expression, showing the stack contents for each step of conversion.
A/(B+C)*D-E

Answer - 11 : -

A/ (B + C) *D-E

Element

Stack

Expression

A

/

(

B

+

C

)

D

E

(

(/

(/C

(/c

(/c+

(/c+

(*

(*

(-

(-

A

A

A

AB

AB

ABC

ABC+

ABC+/

ABC+/D

ABC+/D*

ABC+/D*E-

Question - 12 : -
Write definition for a function DISPMID (int A[][5], int R, int C) in C+ + to display the elements of middle row
and middle column from a two dimensional array A having R number of rows and C number of columns.
For example, if the content of array is as follows:

215 912 516 401 515
103 901 921 802 601
285 209 609 360 172
The function should display the following as output:
103 901 921 802
601 516 921 609

Answer - 12 : -

void DISPMID (int A[] [5] , int R, int C)
 {
 int mid = (R+C)/2;
 for (int i=0; i
 {
 Cout << A[mid] [i]<<"";
 } cout<
 for (int i=0; i
 cout << A[i][mid]<<"";
 }

Question - 13 : -
Convert the following infix expression to its equivalent postfix expression, showing the stack contents for each step of conversion:
X/Y+U*(V-W)

Answer - 13 : -

X / Y + U* (V – W) =((X / Y) + (U * (V – W)))

Element

Stack

Postfix

(

(

X

X

/

/

X

Y

/

XY

)

XY/

+

+

XY/

(

+

XY/

U

+

XY/U

*

+ *

XY/U

(

+ *

XY/U

V

+ *

XY/UV

+ *-

XY/UV

W

+ *-

XY/UVW

)

+ *

XY/UVW-

)

+

XY/UVW-*

)

it

XY/UVW-* +

OR

Element

Stack

Postfix

X

X

/

/

X

Y

/

XY

+

+

XY/

U

+

XY/U

*

+ *

XY/U

(

+ *(

XY/U

V

+ *(

XY/UV

+ *(-

XY/UV

w

+ ‘(-

XY/UVW

)

+ *

XY/UVW-

XY/UW-*

XY/UVW-* +

OR

Any other method orconverting the given Infix expression to its equivalent Postfix expressionshowing stack contents

 

Question - 14 : -
Write member functions to perform POP and PUSH operations in a dynamically allocated stack containing the objects of the following structure:

struct Game
 { char Gamename[30];
 int numofplayer;
 Game *next; } ;

Answer - 14 : -

struct Game
 {
 char Gamename[3 0] ;
 int numofplayer;
 Game *next;
 };
 class Stack { Game *Top;
 public :
 Stack ()
 {
 Top = NULL;
 }
 void Push();
 void Pop();
 void display();
 -Stack();
 } ;
 void Stack::Push()
 {
 Game *temp = new Game;
 cout<<"Enter Data : "; gets(temp->Gamename);
 cin>>temp->numofplayer;
 temp->next =Top;
 Top = temp;
 }
 void Stack:: Pop()
 {
 if ( Top != NULL)
 {
 Game *temp = Top;
 coutnext;
 delete temp;
 }
 else
 cout<<"Stack is empty....";
 }

Question - 15 : -
Write a function PUSHBOOK() in C++ to perform insert operation on Dynamic Stack, which contains Book_no and Book_Title. Consider the following definition of NODE, while writing your C+ + code,

struct NODE
{
int Book_No ;
char Book_Title [20];
NODE * Next;
};

Answer - 15 : -

Void PUSHBOOK (NODE *TOP> int Book_No, char B Title [20])
 {
 NODE*temp;
 temp=new NODE;
 temp —> Book_No=Book_No;
 Strcpy (temp —> Book_Title, B Title) ;
 temp --> Next=NULL ;
 if (Top==NULL)
 Top=temp;
 else
 {
 temp —> Next=top;
 Top==temp;
 }
 }

Question - 16 : -
Write a function POPBOOK( ) in C++ to perform delete operation from a Dynamic Stack, which contains Bno and Title. Consider the following definition of NODE, while writing your C++code.

struct NODE
{
int Bno;
char Title[20] ;
NODE * Link;
} ;

Answer - 16 : -

node*PopBOOK(node*TOP int Bno, char B Title [20])
{
node*temp;
temp=new node;
temp —>Bno=Bno;
strcpy (temp —>Title, B Title);
temp ->link=NULL:
if (TOP==NULL)
Top=Temp;
else
{
temp —>link=Top;
TOP==temp;
}
}

Question - 17 : -
Write the definition of a member function push() for a class Library in C++ to insert a book information in a dynamically allocated strack of books considering the following code is already written as a part of the program

struct book
{
int bookid;
char bookname[20];
book*next;
} ;
class Library
{
book*top;
public
Library()
{
top=NULL;
}
void push();
void pop();
void disp() ;
-Library();
};

Answer - 17 : -

void Library: :push()
{
book*nptr;
nptr=new book;
cout<<"Enter values for bookid and bookname"; cin> >nptr->bookid;
gets(nptr->bookname);
nptr->next =NULL;
if (top==NULL)
top=nptr;
else
{
nptr->next=top,
top=nptr;
}
}

×