Stack and QUEUE QUES WITH SOLUTION
Stack and QUEUE QUES WITH SOLUTION
56.
60. Write the equivalent infix expression for a, b, AND, a, c, AND, OR.
Ans 60. a, b, AND, a, c, AND, OR
(a AND b), (a AND c), OR
(a AND b) OR (a AND c)
63. Give postfix form expression for: NOT A OR NOT B AND NOT C
Ans 63. =((A NOT) OR ((B NOT) AND (C NOT)))
=((A NOT) OR ((B NOT C NOT AND))
=A NOT B NOT C NOT AND OR
66. Evaluate the following postfix expression. Show the status of stack
after execution of each operation separately;
F, T, NOT, AND, F, OR, T, AND
Ans 66. F, T, NOT, AND, F, OR, T, AND
Scanned Element Operation Stack Status
F Push F
T Push F, T
NOT Pop one operand from stack F
NOT T = F
Push F, F
AND Pop two operands from stack
F AND F = F
Push F
F Push F, F
OR Pop two operands from stack
F OR F = F
Push F
T Push F, T
AND Pop two operands from stack
F AND T = F
Push
Pop all F
Result F
67. Evaluate the following postfix expression. Show the status of stack
after execution of each operation separately;
T, F, NOT, AND, T, OR, F, AND
Ans 67. T, F, NOT, AND, T, OR, F, AND
Scanned Element Operation Stack Status
T Push T
F Push T, F
NOT Pop one operand from stack T
NOT F = T
Push T, T
AND Pop two operands from stack
T AND T = T
Push T
T Push T, T
OR Pop two operands from stack
T OR T = T
Push T
F Push T, F
AND Pop two operands from stack
T AND F = F
Push F
Result F
68. Evaluate the following postfix expression. Show the status of stack
after execution of each operation:
5, 2, *, 50, 5, /, 5, –, +
Ans 68. 5, 2, *, 50, 5, /, 5, –, +
Scanned Element Stack Status
5 5
2 5.2
* 10
50 10, 50
5 10, 50, 5
/ 10, 10
5 10, 10, 5
– 10, 5
+ 15
69. Evaluate the following postfix expression. Show the status if stack
after execution of each operation;
60, 6, /, 5, 2, *, 5, –, +
Ans 69. 60, 6, /, 5, 2, *, 5, –, +
Scanned Element Stack Status
60 60
6 60, 6
/ 10
5 10, 5
2 10, 5, 2
* 10, 10
5 10, 10, 5
– 10, 5
+ 15
70. Evaluate the following postfix expression using a stack and show the
contents of stack after execution of each operation;
5, 3, 2, *, 4, 2, /, –, *
Ans 70. 5, 3, 2, *, 4, 2, /, –, *
Scanned Element Stack Status
5 5
3 5, 3
2 5, 3, 2
* 5, 6
4 5, 6, 4
2 5, 6, 4, 2
/ 5, 6, 2
5, 4
* 20
71. Evaluate the following postfix notation. Show status of stack after
every step of evaluation (i.e. after each operator):
False, NOT, True, AND, True, False, OR, AND
Ans 71. False, NOT, True, AND, True, False, OR, AND
Scanned Operation Stack
Element Status
False Push onto stack. False
NOT Pop one element from the
stack i.e. False. Apply NOT on
False we get true, push onto stack.
True Push onto stack. True, True
AND Pop two elements from the
stack. Apply AND between
them. Push result onto stack.
True Push True onto stack True, True
False Push False onto stack. True, True, False
OR Pop two elements from the True, True
stack. Apply OR between them
and push result onto stack.
AND Pop two elements from the True
stack. Apply and between them
and push result onto stack.
4 Marks Questions
78. 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();
};
Ans 78. void STACK::POP()
{
if (Top!=NULL)
{
TEXTBOOKS *Temp;
Temp=Top;
cout<<Top->ISBN<<Top->TITLE<<”deleted”<<endl;
Top=Top>
Link;
delete Temp;
}
else
cout<<”Stack Empty”<<endl;
}
79. Write the definition 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();
};
Ans 79. void STACK::PUSH()
{
BOOKS *Temp;
Temp=new BOOKS;
gets(Temp->ISBN);
gets(Temp->TITLE);
Temp->Link=Top;
Top=Temp;
}
struct NODE
{ char City[20];
NODE *Next;
};
class Queue
{ NODE *Rear,*Front;
puplic:
Queue( )
{ Rear=NULL;Front=NULL;
}
void Qinsert( );
void Qdelete( );
void Qdisplay( );
~Queue( );
} ;
void Queue::Qinsert( )
{
NODE *Temp;
Temp=new NODE;
cout<<”Data:”;
gets (Temp->City);
Temp->Next=NULL;
if (Rear==NULL)
{
Rear=Temp;
Front=Temp;
}
else
{
Rear–>Next=Temp;
Rear=Temp;
}
}
void Queue::Qdelete( )
{
if (Front!=NULL)
{
NODE *Temp=Front;
cout<<Front->City<<”Deleted \n”;
Front=Front->Next;
delete Temp;
if (Front==NULL)
Rear=NULL;
}
else
cout<<”Queue Empty..”;
}
Queue:: Qdisplay( )
{ NODE *Temp=Front;
while (Temp!=NULL)
{
cout<<Temp->City<<endl;
Temp=Temp->Next;
}
}
Queue:: ~Queue( )//Destructor Function
{ while (Front!=NULL)
{ NODE *Temp=Front;
Front=Front->Next; delete Temp;
}
}
void main( )
{ Queue QU;
char Ch;
do
{
:
:
} while (Ch!=’Q’);
}
};
Ans 87. void QINSERT()
{ NODE *P=new NODE();
cout<<"Enter the client number";
cin>>P->Cno;
cout<<"enter the client name";
gets(P->Cname);
P->Next=NULL;
if(front==NULL && rear==NULL)
{ front=P;
rear=P;
}
else
{ rear->Next=P; rear=P; } }
88. Write a function PUSHBOOK() in C++ to perform insert operation on a
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;
};
Ans 88. void PUSHBOOK(NODE *top)
{ NODE *NEW=new NODE;
cout<<"Enter the book number";
cin>>NEW->Book_No;
cout<<"Enter book title";
gets(NEW->Book_Title);
NEW->Next=NULL;
if(top==NULL)
top=NEW;
else { NEW->Next=top; top=NEW;}
}
89. Write a function POPBOOK() in C++ to perform delete operation on a
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 *Link;
};
Ans 89. void POPBOOK(NODE *top)
{
cout<<"deleting top element from stack\n";
cout<<"Book No"<<top->Book_No<<endl;
cout<<"Book title"<<top->Book_Title<<endl;
NODE *temp=top;
top=top->Link;
delete(temp);
}
Ans 90.
void DELETE()
{ Node *temp;
if(front==NULL) // No element in the queue
{ cout<<”UNDERFLOW………………..”;
}
else
{ temp=front;
front=front->Link;// Making the second node as the first one
temp->Link = NULL;
delete temp; // deleting the previous first node.
}
}
Ans 92.
Algorithm for user choice:
1. ch=0 // Initialize a variable for user choice
2. Read ch //Enter, user choice 1 for push and 2 for pop
3. If(ch == 1) then /* if top is at end of Array-Queue */
4. call insert function
5. Else
6. call delete function
7. End
Algorithm for inserting in Linked-Queue:
/* Allocate space for ITEM to be inserted */
1. NEWPTR = new node
2. NEWPTR -> INFO = ITEM; NEWPTR -> LINK=NULL
/* Insert in the queue */
3. If rear = NULL Then
{
4. front = NEWPTR
5. rear = NEWPTR
6. else
{
7. rear -> LINK = NEWPTR
8. rear=NEWPTR
}
9. END.
Ans 93.
Algorithm for user choice:
1. ch=0 // Initialize a variable for user choice
2. Read ch //Enter, user choice 1 for push and 2 for pop
3. If(ch == 1) then /* if top is at end of Array-Queue */
4. call insert function
5. Else
6. call delete function
7. End
ANs 94.
Algorithm for user choice:
1. ch=0 // Initialize a variable for user choice
2. Read ch //Enter, user choice 1 for push and 2 for pop
3. If(ch == 1) then /* if top is at end of stack-array */
4. call push function
5. Else
6. call pop function
7. End
Algorithm for pushing in Linked-Stack
Step-1: If the Stack is empty go to step-2 or else go to step-3
Step-2: Create the new element and make your "stack" and "top" pointers
point to it and quit.
Step-3: Create the new element and make the last (top most) element of the
stack to point to it
Step-4: Make that new element your TOP most element by making the "top"
pointer point to it.
Algorithm for popping in Linked-Stack:
Step-1: If the Stack is empty then give an alert message "Stack Underflow"
and quit; or else proceed
Step-2: If there is only one element left go to step-3 or else step-4
Step-3: Free that element and make the "stack", "top" and "bottom"
pointers point to NULL and quit
Step-4: Make "target" point to just one element before the TOP; free the
TOP most element; make "target" as your TOP most element
Ans 95.
Ans 96.
1. If (F<0) then
{
write(“Queue Underflow”)
goto step 4
}
2. DATA=Q[F]
3. If(F=R) then
{ F=R=-1 }
else
{
if(F=N-1)
F=0
else
F=F+1
}
4. End