You are on page 1of 5

Subject code & Name: CS6202 Programming and Data Structures-I

Part-A Questions and Answers


Unit-IV
1. Define Stack ADT. What are the operations on it?
Stack is an ordered collection of element in which insertion & deletion operations are
restricted to one end of list.
The end from which elements are added or removed to or from the list is referred as a
TOP of Stack.
It is also referred as Push-Down List or Piles. The logic of stack is referred as
Last-In-First-Out (LIFO) or First-In-Last-Out (FILO).
OPERATIONS ON STACK ADT
PUSH operation insert new element onto the top of stack.
POP operation deletes an element from the top of stack.
PEEK operation displays the top element of stack.
2. Define Overflow and underflow conditions in stack.
Underflow Condition Checking whether the stack is empty
Overflow Condition Checking whether the stack is full (i.e.) number of elements in
stack is equal to its size.
3. What is a constraint for fully loaded stack and empty stack?
Fully Occupied Stack: if(top= =Stack_SIZE-1) then "Stack is Full
Empty Stack: if(top= = -1) then "Stack is Empty
Care must be taken in handling extreme cases that are dont PUSH an element onto
the Fully Occupied Stack (there is no storage space in stack to perform insert) and
dont POP an element from the Empty Stack (there is no more element present in the
stack).
4. List the applications of Stack.
Tower of Hanoi
Reverse a string
Balanced parenthesis (or) Balancing symbol
Conversion from infix expression to postfix expression
Evaluation of postfix expression
Function calls
5. State the process of tower of Hanoi?
It is a game which contains three Shafts named as A, B and C. Based on risk levels,
minimum to maximum number of Disc will be placed on the Shaft A.
Discs are varying in size from large to small (i.e.) small disc is placed above the large
one. The goal is to transfer the Discs from Shaft A to Shaft C using the Shaft B.
The constraints to play the game are,
Only one Disc should be moved at a time.
No larger Disc should be placed above the smaller one while moving discs.
Top Disc in shaft should be removed first.
6. What do you meant by reverse polish notation and its need?
Postfix expression is also referred as Reverse Polish Notation. Format of
expression is: Operand1 Operand2 (Operator). Eg: AB+
Need of Postfix expression
Dont worry about the rules of Operator Precedence.

Dont worry about the rules for Right to Left Associativity.


7. List down the types of expression in C.
Expression types
Expression Format
Example
1. Infix
Operand1 (Operator) Operand2
A+B
2. Prefix (Polish
(Operator) Operand1 Operand2
+AB
Operand1 Operand2 (Operator)
AB+
Notation)
3. Postfix (Reverse
Polish Notation)
8. What happen when a function call is made?
When a call is made to a new function,
1. All the variables local to the Caller routine must be saved by the system,
since the Called function will overwrite the Caller routine variables.
2. Furthermore the current location of Caller routine must be saved, so that the
Called function knows where to go after it completes the process.
When a function is called, important data such as register value and the return address
is saved and put at the top of a Pile or Stack. Then the control is transferred to the
Called function. Those saved data will be called as Activation record or Stack frame.
9. Define tail recursion.
Tail recursion refers to a recursive call written at the last line of a function. It can be
mechanically eliminated by changing the recursive call to a goto statement preceded
by one assignment statement per function argument.
Example
void PrintList (list L)
{
if(L!=NULL) --------------------------- Line 1
{
printf(%d,L->element);--------- Line 2
PrintList (L->next);---------------- Line 3
}
}
10. Define Queue ADT. What are the operations on it?
Queue ADT has an ordered collection of element in which insertion is made at one
end of list & deletion is made at the other end.
The queue has two ends. The end at which insertions are made is referred to as the
REAR end. The end from which deletions are made is referred to as the FRONT
end.
In queue, the first inserted element will be removed first. So a queue is referred as
First-In-First-Out (FIFO) or LILO (Last-In-Last-Out) ADT.
OPERATIONS ON QUEUE ADT
1. Enqueue Insert a new element onto the queue.
2. Dequeue Delete an element from the queue.
11. List down the types of queue.
Linear Queue
Circular Queue.
Deque Queue
Priority Queue.
12. Differentiate Stack and Queue.

Stack ADT
Queue ADT
1. It is an ordered list where insertion and 1. It is an ordered collection of element where
deletion operation are performed at one end
insertion operation is performed at rear end
called Top of stack.
and deletion operation is performed at front
end of queue.
2. Stack is based on LIFO logic.
2. Queue is based on FIFO logic.
3. Associated with stack there is one variable 3. Associated with queue there are two
called Top.
variable named as front and rear.
4. Insertion & deletion operations are referred
4. Insertion and deletion operations are
as Enqueue & Dequeue respectively.
referred as PUSH & POP respectively.
5. Fully occupied queue is identified by rear =
5. Fully occupied stack can be identified by
= Qsize-1
Top = =Stacksize-1
6. To insert an element rear is incremented by
6. To insert an element Top is incremented by
one.
one.
7. To delete an element front is incremented
7. To delete an element Top is decremented
by one.
by one.
13. What is a constraint for fully loaded queue and empty queue?
If the number of elements stored onto the queue is equal to the queue size then the
queue is called as Fully Occupied Queue (i.e.) Rear < (Qsize 1)
If there is no element in queue then it is called as Empty Queue (i.e.) Front= = -1
14. Define linear queue and its demerits.
It is a simple queue structure. Linear queue is mostly implemented by using Array
Data Structure.
During the enqueue operation, the rear variable is incremented by one and new
element is placed on the rear position of queue.
During the Dequeue operation, the front variable is incremented by one.
Demerits
In linear queue, the Enqueue operation is performed irrespective of the position of
front variable that is after the deletion of some elements from the queue, it wont be
fully occupied queue but we cant perform Enqueue of new element onto the queue.
So the Queue size is not properly utilized i.e. memory is wasted.
15. Define circular queue with neat sketch.
It is same as linear queue except that the structure of queue becomes circularly
connected. So that, the memory space of dequeued element is also used for enqueue
until the queue is full.
To insert element, the rear pointer is incremented by one and the new element is
stored on the position [rear % Qsize]. During the Dequeue operation, the front
variable is incremented by one.
Qsizewhen
= 8 difference of rear and front is equal to
The queue is said to be fully occupied
the queue size minus one (rear-front= =Qsize-1).
1

Front = 0
0
START of QUEUE

2
3
4

Rear = 4

16. What is deque and possible operations on it?


It is another form of queue in which insertion & deletion are made at both end (the
front & rear) of the queue. It is also referred as Double ended queue. There are 2
variation of deque, namely input restricted deque and output restricted deque.
1. Input Restricted Deque - Allow insertion at only one end and deletion in both
end of queue.
2. Output Restricted Deque - Allow deletion at only one end and insertion in both
end of queue.
Rear

Front
Deletio
n

..
Input-Restricted
Deque
Rear
Front

Deletio
n

..

Deletio
n

Insertio
n

Output-Restricted
Deque

17. Define priority queue.


It is a collection of elements and each element contains a key referred as priority.
Element can be inserted in any order (i.e. elements are placed onto the queue in
alternating priority).
The element are deleted from the queue only based on the order of their priority (i.e.,
the element with higher priority is deleted first and so on). The elements having same
priority are consider being equal importance & processed accordingly.
18. Compare Linear and Circular Queue.
Queue Type
Fully
occupie
d queue

Linear Queue

Circular Queue
Empty
queue

Enqueu
e

Linear Queue

Circular Queue
Linear Queue

Using Array Data


Structure
Rear value is equal to the
(Qsize-1) (i.e., total no. of
element in the queue is
equal to Qsize)
(Rear-Front) value is equal to
the (Qsize-1)
Rear=front=-1(i.e. there is no
element present in the queue)
Same as Linear Queue
After increment the rear
value by one, the new
element is placed at the rearth
index in array

Using Linked list data


structure
When the node is not create
properly by malloc( ) i.e. it
return NULL value then it
means that the queue is full
Same as Linear Queue
If front pointer is equal to
NULL then the queue
becomes empty
Same as Linear Queue.
A new node is inserted at
last of linked list using rear
pointer

Circular Queue

After increment the rear


value by one, the new
element is placed at the (rear
% Qsize)th index in array

Same as Linear Queue but


here the last node is
circularly connected with
the first node.

19. List any three major applications of Queue.


When jobs are submitted to a networked printer, they are arranged in order of arrival,
so jobs are placed on a queue only.
Virtually every real-life line is a queue. For example, line at ticket counters are queue,
because service is based on first come-first-served.
Calls to large companies are generally placed on a queue when all operators are busy.
For example, jobs in Business Process Outsourcing.
20. What are the elements remaining in stack and queue with size 10 after the
following operations? Insert: 10,40,20,50,90; Delete:2 elements; Insert: 100, 200;
Delete 3 elements.
Stack
Elements in stack after push (10,40,20,50,90): 90,50,20,40,10
Elements in stack after pop (2): 20,40,10
Elements in stack after push (100,200): 200,100,20,40,10
Elements in stack after pop (3): 40,10
Queue
Elements in queue after enqueue (10,40,20,50,90): 10,40,20,50,90
Elements in queue after dequeue (2): 20,50,90
Elements in queue after enqueue (100,200): 20,50,90,200,100,
Elements in queue after dequeue (3): 200,100

You might also like