UNIT-III Stacks and Queues
UNIT-III Stacks and Queues
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-III : Stacks and Queues: Total Marks: 20
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
2. Describe Queue as an abstract data type
Ans: Try to write answer as per question 1.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
4. Describe following terms with suitable diagram: i) Stack overflow ii) Stack underflow.
Ans:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
5. Describe following terms with suitable diagram: i) Queue overflow ii) Queue underflow.
Ans: Try to write answer as per question 4.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
7. Explain four primitive operations on stack.
Ans:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
8. Show the memory representation of Stack using array with the help of a diagram.
Ans:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
9. Describe push and pop operations on stack. Also write algorithm for push and pop operations.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
10. Recursion is one of the application of stack-YES/NO ? Explain it for calculating the factorial of a
number 5 using recursion
Ans: Yes, Recursion is one of the application of stack
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
11. Convert the following infix expression to its postfix form using stack A + B – C*D/E + F.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
12. Convert following infix expression into a postfix expression. Show all steps. (P + (Q * R – (S/T
↑ U) * V) * W)
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
13. Convert the given infix expression in to prefix expression. Write all steps for conversion (a * b
+ c/d) * (e + f g).
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
14. Convert the following infix expression to its prefix form using stack A + B ^ C*(D/E) – F%G.
Show diagrammatically each step of conversion.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
15. Translate below expression into prefix:
After that evaluate the above prefix expression using below values.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
16. Evaluate the following prefix expression: - * + 4 3 2 5 Show diagrammatically each step of
evaluation using stack.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
17. Show the effect of PUSH and POP operation on to the stack of size 10. The stack contains 10,
20, 22, 26, 28, and 30, with 30 being at top of the stack. Show diagrammatically the effect of
1. PUSH 46
2. PUSH 48
3. POP
4. POP
5. POP
Sketch the final structure of Stack after performing the above said operations. Sketch the final
structure of Stack after performing the above said operations.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
18. Write an algorithm for performing push operation on stack.
Ans:
Step 1: Start
Step 2: if(top == SIZE-1)
print “Stack is Full” and goto step 5.
otherwise
goto Step 3.
Step 3: Increment top variable i.e top=top+1
Step 4: Push new element onto stack i.e data[top]=x;
Step 5: Stop
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
19. Show the effect of INSERT and DELETE operations on to the linear queue of size 5.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
20. Draw the diagram of queue to represent front and rear pointers of queue.
Ans:
Otherwise
Step 4: Stop
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
22. Explain concept of priority queue with example and its advantages.
Ans:
void initialize();
int empty();
int full();
void insertion();
void deletion();
void print();
int main()
{
int ch;
initialize();
do
{
printf("\n****Queue MENU****");
printf("\n1. Insertion");
printf("\n2. Deletion");
printf("\n3. Print");
printf("\nEnter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insertion();
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
break;
case 2: deletion();
break;
case 3: print();
break;
case 4: printf("\nThanks for using our software");
break;
default:printf("\nInvalid choice");
}
}while(ch!=4);
return 0;
}
void initialize()
{
rear=front=-1;
}
int empty()
{
if(rear==-1)
return(1);
else
return(0);
}
int full()
{
if(rear==MAX-1)
return(1);
else
return(0);
}
void insertion()
{
int x;
if(full()==0)
{
printf("\nEnter Data:");
scanf("%d",&x);
if(rear==-1)
{
rear=front=0;
data[rear]=x;
}
else
{
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
rear=rear+1;
data[rear]=x;
}
printf("\nData inserted Successfully");
}
else
{
printf("\nQueue is FULL");
}
}
void deletion()
{
int x;
if(empty()==0)
{
x=data[front];
if(front==rear)
{
rear=front=-1;
}
else
{
front=front+1;
}
printf("\nDeleted Data: %d",x);
}
else
{
printf("\nQueue is EMPTY");
}
}
void print()
{
int i;
printf("\nQueue Data:");
for(i=front;i<=rear;i++)
{
printf("%d\t",data[i]);
}
}
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
24. Write a program for insert and delete operation perform on queue. State any two application
of queue.
#include <stdio.h>
#define MAX 5
int data[MAX],rear,front;
void initialize();
int empty();
int full();
void insertion();
void deletion();
void print();
int main()
{
int ch;
initialize();
do
{
printf("\n****Queue MENU****");
printf("\n1. Insertion");
printf("\n2. Deletion");
printf("\n3. Print");
printf("\nEnter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: print();
break;
case 4: printf("\nThanks for using our software");
break;
default:printf("\nInvalid choice");
}
}while(ch!=4);
return 0;
}
void initialize()
{
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
rear=front=-1;
}
int empty()
{
if(rear==-1)
return(1);
else
return(0);
}
int full()
{
if(rear==MAX-1)
return(1);
else
return(0);
}
void insertion()
{
int x;
if(full()==0)
{
printf("\nEnter Data:");
scanf("%d",&x);
if(rear==-1)
{
rear=front=0;
data[rear]=x;
}
else
{
rear=rear+1;
data[rear]=x;
}
printf("\nData inserted Successfully");
}
else
{
printf("\nQueue is FULL");
}
}
void deletion()
{
int x;
if(empty()==0)
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
{
x=data[front];
if(front==rear)
{
rear=front=-1;
}
else
{
front=front+1;
}
printf("\nDeleted Data: %d",x);
}
else
{
printf("\nQueue is EMPTY");
}
}
void print()
{
int i;
printf("\nQueue Data:");
for(i=front;i<=rear;i++)
{
printf("%d\t",data[i]);
}
}
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
25. Write a code delete an element in queue.
Deletion() :
In a queue, deletion() is a function used to delete an element from the queue. The
element is always deleted from front end.
Before deleting a new element from the queue first needs to be check whether queue is
empty or not if queue is not empty then and then only, we can delete an element from
the queue otherwise we need to print “Queue is Empty” message.
Below steps are used to delete an element from the queue.
Algorithm:
Step 1: Start
Step 2: Check queue is Empty or not. If empty then display message “Queue is empty”
and goto step 7.
Step 3: Delete front element from the queue i.e X=data[front]
Step 4: if(front == rear) then
set -1 to rear and front variables.
otherwise
goto Step 5.
Step 7: Stop.
C Function:
void deletion()
{
int x;
if(empty()==0)
{
x=data[front];
if(front==rear)
{
rear=front=-1;
}
else
{
front=front+1;
}
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
printf("\nDeleted Data: %d",x);
}
else
{
printf("\nQueue is EMPTY");
}
}
26. Define the terms FRONT and REAR of queue. Enlist any four operations of queue.
Ans:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
27. Draw and explain circular queue.
Ans:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30