You are on page 1of 14

Unit 2

Stacks: Definition, Array representation of stacks, Linked representation of stacks, Stack


as ADT, Arithmetic Expressions: Polish Notation, Conversion of infix expression to
postfix expression, Evaluation of Post fix expression, Application of Stacks, Recursion,
Towers of Hanoi, Application of Stacks, Queues: Definition, Array representation of
queue, Linked list representation of queues. Types of queue: Simple queue, Circular
queue, Double-ended queue, Priority queue, Operations on Queues, Applications of
queues.

What is stack? Explain. 5m


A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, i.e., the
item which is added at last is removed first. The best analogy for a stack is either a pile of
coins or a pile of books kept one above the other. You can add or remove an item from the
top only.

What are the various Operations on stack. 2m

 push(): It inserts an element to the top of the stack. It takes O(1) time, as each node is
inserted at the head/top of the linked list.
 pop(): It removes an element from the top of the stack. It takes O(1) time, as the top
always points to the newly inserted node.
 peek(): It returns the top element of the stack.
 size(): It returns the size of the stack, i.e., the total number of items in a stack.
 Isfull(S) : to check whether the Stack 'S' is full.

Isempty(S) : to check whether the Stack 'S' is empty.

Explain Array representation of Stack. 5m


• Stack can be represented by means of a one way list or a linear array.
• A pointer variable top contains the locations of the top element of the stack and a variable
MAXSTK gives the maximum number of elements of the Stack .
• the condition top=-1 will indicate that the stack is empty.
PUSH

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 1


POP
TOP=-1
Stack underflow
Stack overflow

DIS-ADVANTAGES
A stack can be implemented using arrays. But arrays are of limited size, and the size of the stack
has to be predetermined, whereas, in a linked list, implementation nodes can be added according
to the user's requirements.

Linked list representation of stack

A stack is represented using nodes of a linked list. Each node consists of two parts: data and
next(storing the address of the next node). The data part of each node contains the assigned
value, and the next points to the node containing the next item in the stack. The top refers to the

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 2


topmost node in the stack. Both the push() and pop() operations are carried out at the front/top of
the linked list .

Mention the applications of stack.2m


• Recursion
• Reversal of a string
• Checking the parenthesis matching
• Postfix expression evaluation
• Infix to postfix conversion
• Infix to prefix conversion
• Expression evaluation
• Backtracking
• Memory Management
• Function calling and return

Write the procedure for PUSH, POP AND DISPLAY. 5M


Procedure push(stack, top,maxstk,item)
Step1: [Overflow check]
if (top == maxstk -1) then
write (“stack overflow ")
return;
Step2: [increment top value]
top = top + 1
Step 3: [insert the item at the top]
stack[top] = item
Step 4: return;

Procedure pop(stack, top,item)


DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 3
Step1: [Underflow check]
if (top == -1) then
write (“stack underflow ")
return;
Step2: [delete the item]
item = stack[top]
Step 3: [decrement top value]
top = top - 1
Step 4: return;

Procedure display()
{
if(top == -1)
{
printf("stack is empty \n");
exit(0);
}
else
{
printf("stack contents are \n");
for(i=top;i>=0;i--)
printf("elements are %d \n",stack[i]);
}
}

Explain Polish and reverse polish Notation of Arithmetic Expressions. 5m


Briefly explain infix, prefix and postfix expressions.

• The stack organization is very effective in evaluating arithmetic expressions.


• Expressions are usually represented in what is known as Infix notation, in which each
operator is written between two operands (i.e., A + B).
• With this notation, we must distinguish between ( A + B )*C and A + ( B * C ) by using
either parentheses or some operator-precedence convention.
• Thus, the order of operators and operands in an arithmetic expression does not uniquely
determine the order in which the operations are to be performed.

• Arithmetic Expressions involving operators and operands can be classified three types
depending on how the operator is placed between operands.
1. Infix notation
2. Prefix notation (polish notation

3. Postfix notation (reverse polish notation)


DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 4
1. Polish notation (prefix notation) –

It refers to the notation in which the operator is placed before its two operands . Here no
parentheses are required, i.e.,

+AB

2. Reverse Polish notation(postfix notation) –

It refers to the notation in which the operator is placed before its two operands . Here no
parentheses are required, i.e.,

AB+

• Stack organized computers are better suited for post-fix notation than the traditional infix
notation. Thus the infix notation must be converted to the post-fix notation. The
conversion from infix notation to post-fix notation must take into consideration the
operational hierarchy.
• There are 3 levels of precedence for 5 binary operators as given below:
• Highest: Exponentiation (^) Next highest: Multiplication (*) and division (/) Lowest:
Addition (+) and Subtraction (-)

Algorithm to convert infix to postfix expression


ALGORITHM POLISH(Q,P)
suppose Q is an arithmetic expression written in infix notation,This algorithm finds the
equivalent postfix expression.

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 5


DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 6
DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 7
PROBLEM: 6,2,3,+,-,3,8,2,/,+,*,2,1,3,+,*,*
ans 56

PROBLEM : 5,3,+,6,2,/,*,3,5,*,+
ANS - 39

What is recursion? 2m
 Recursion is a powerful programming tool, that allows to execute the function repeatedly
in terms of itself.
or
 “Recursion is a process of calling the function repeatedly in terms of itself. The repeated
calling is terminated by the specific condition”.

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 8


What are the Properties of recursive functions 2m
1.Function must have a stopping condition.
ie it should not continue to call indefinitely.
2. Each time the function calls itself, it must be in recursive form.

What are the types of recursion? 2m


 Direct – in direct recursion, the function calls itself repeatedly until certain condition is
satisfied.
 Indirect – In this type, the function calls another function which eventually causes the
same function to be called.

Explain Towers of Hanoi problem. 5m

 is a mathematical game or puzzle. It consists of three rods, and a number of disks of


different sizes which can slide onto any rod.

 The objective of the puzzle is to move the entire stack to another rod, obeying the
following rules:
 Only one disk may be moved at a time.
 Each move consists of taking the upper disk from one of the rods and sliding it onto
another rod, on top of the other disks that may already be present on that rod.
 No disk may be placed on top of a smaller disk.

Algorithm Hanoi(n,s,i,d)
// s -> source peg, d->destinations,
//i-> intermediate peg
// input: no of discs n
// output: the sequence of movements.
{
if (n==1)
move disk s to d
else
Hanoi(n-1,s,d,i);
move disk from s to d;
Hanoi(n-1,i,s,d);
}

 Move the top N - 1 disks from Src to Aux (using Dst as an intermediary peg)
 Move the bottom disk from Src to Dst
 Move N - 1 disks from Aux to Dst (using Src as an intermediary peg)

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 9


QUEUES

 Definition
 Array representation of queue
 Types of queue: Simple queue
 Circular queue
 Double ended queue(deque)
 Priority queue
 Operations on all types of queues
What is a linear queue?
• A Queue is an ordered collection of items.
• Items are deleted at one end called front and inserted at the other end called rear of
the queue.
• The first element inserted into the queue is the first element to be removed. Also called as
FIFO.

Explain Array representation of queue 5m


• Queue can be represented as a linear array called QUEUE with 2 variables.
• FRONT : Contains the index of the front element.
• REAR: Contains the index of the rear element.
• Initially REAR = -1, FRONT = 0
• So, when first item is added FRONT = REAR = 0

Conditions
• REAR = FRONT - 1 will indicate queue is empty
• REAR = FRONT , only 1 element in queue.
• REAR = N -1 , Where N is array size, queue is full

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 10


Explain the different operations on Queue 2m
1. INSERTION INTO A QUEUE
2. DELETION FROM A QUEUE
3. DISPLAY
4. QUEUE FULL
5. QUEUE EMPTY

ALGORITHM Insertion into a Queue


procedure Qinsert(Q,FRONT,REAR,MAX,ITEM)
STEP1: [Queue is full check]
if REAR=MAX - 1 then
write “QUEUE IS FULL”
return
STEP2: [Increment REAR pointer]
REAR = REAR + 1
STEP3: [Insert item]
Q[REAR] = ITEM
STEP 4: return

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 11


ALGORITHM Deletion from a Queue
procedure Qdelete(Q,FRONT,REAR,N,ITEM)
STEP1: [Queue is empty check]
if REAR=FRONT-1 then
write “QUEUE IS EMPTY”
return
STEP2: [Delete the item]
ITEM = Q[FRONT]
STEP3 : if FRONT = REAR [When there is only one item]
FRONT = 0
REAR = -1
else
FRONT = FRONT +1
STEP 4: return

EXPLAIN THE TYPES OF QUEUE. 5M


1. SIMPLE OR LINEAR QUEUE (above explanation)
2. CIRCULAR QUEUE
3. DOUBLE ENDED QUEUE
4. PRIORITY QUEUE

2. CIRCULAR QUEUE

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 12


• Circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first
position to make a circle. It is also called 'Ring Buffer'. In a normal Queue, we can insert
elements until queue becomes full.

3. PRIORITY QUEUE

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 13


A priority queue is a type of queue that arranges elements based on their priority values.
Elements with higher priority values are typically retrieved before elements with lower priority
values.
In a priority queue, each element has a priority value associated with it. When you add an
element to the queue, it is inserted in a position based on its priority value. For example, if you
add an element with a high priority value to a priority queue, it may be inserted near the front
of the queue, while an element with a low priority value may be inserted near the back.
Properties of Priority Queue
So, a priority Queue is an extension of the queue with the following properties.
 Every item has a priority associated with it.
 An element with high priority is dequeued before an element with low priority.
 If two elements have the same priority, they are served according to their order in the
queue.

Double ended queue(deque)

• In deque, insertions and deletions can be made at either end but not in the middle.
• Types of Deque:
• Input Restricted Deque: allows the insertions at only one end but allows deletions at
both ends.
• Output Restricted Deque: allows the deletions at only one end but allows insertion at
both ends.

Applications of Queue

• Simulation- modeling of a real life problem


• Buy a movie ticket
• Bank/ATM
• Queues are used in time sharing systems in which programs form a queue while waiting
to be executed.
• Circular queues are used in operating system,
• Queues are used in network communication systems.

DS UNIT2 NOTES (STACKS and QUEUES) Faculty: Ms. Anitha Page 14

You might also like