DS Unit-2
DS Unit-2
DS Unit-2
HIMAGIRI
UNIT - II
----------------------------------------------------------------------------------------------------------
Stacks-Operations, array and linked representations of stacks, stack applications-infix
to postfix conversion, postfix expression evaluation, recursion implementation.
Queues-operations, array and linked representations. Circular Queue operations,
Dequeue, applications of queue.
----------------------------------------------------------------------------------------------------------
Stack :
A stack is a linear data structure, which works on the principle of LIFO/FILO.
Stack is closed at one end and opened at other end.
The elements can be added and removed from the stack only at the top(opened end).
Basic Operations performed on stack are:
PUSH-Insertion
POP-Deletion
When an element is pushed into the stack TOP value is incremented by 1 i,e TOP++.
When an element is popped from the stack TOP value is decremented by1 I,e TOP--.
Here, the maximum size(MAX) of the stack is 10 i,e it can hold up to 10 elements.
Basic Operations performed on stack implemented using arrays are:
PUSH
POP
PEEK
2
Prepared by D.HIMAGIRI
Stack Implementation using Array…
PUSH:
The push operation is used to insert an element into the stack. The new element is added at
the topmost position of the stack.
While inserting the value, we must first check if TOP=MAX–1, because if that is the
case, then the stack is full and no more insertions can be done .This condition is called as
stack overflow .
If the above condition fails then we can perform the insertion into the stack , then TOP
value is incremented by 1.
3
Prepared by D.HIMAGIRI
Stack Implementation using Array…
POP:
The POP operation is used to delete an element from top of the stack.
Before deleting an element we have to check whether TOP=-1 ,if that is the case POP
operation is not possible . This condition is called as stack underflow.
Other wise we can pop an element from the stack by decrementing the TOP by 1.
4
Prepared by D.HIMAGIRI
Stack Implementation using Array…
PEEK:
PEEK is an operation that returns the value of the topmost element of the stack without
deleting it from the stack.
If we perform PEEK operation on the stack ,it will first check
whether the stack is empty or not, if stack is not empty then it returns the
top most element in the stack i,e 5 other wise it has to print stack is empty.
5
Prepared by D.HIMAGIRI
Stack Implementation using Linked List
All insertions and deletions are done at the node pointed by TOP.
A linked stack supports all the three stack operations, that is, push, pop, and peek.
6
Prepared by D.HIMAGIRI
Stack Implementation using Linked List….
PUSH:
The push operation is used to insert an element into the stack. The new element is
added at the topmost position of the stack.
Push 5
New Node
TOP
Push 6
TOP
7
Prepared by D.HIMAGIRI
Stack Implementation using Linked List….
POP
The pop operation is used to delete the topmost element from a stack.
Before deleting the element, we must first check
if TOP=NULL (stack empty).
If an attempt is made to delete a value from a stack
that is already empty, an Underflow message is printed.
If TOP!=NULL , then top most element is deleted from
the stack i,e in this case element 1 is deleted from the
stack.
8
Prepared by D.HIMAGIRI
Stack Implementation using Linked List….
PEEK
It is used to print the top element of stack.
If TOP=NULL returns stack is empty, else prints the top element in the linked stack.
If Peek operation is performed on the above stack ,it will return element 1.
Step 1:if(top==NULL)
return “stack is empty”;
Step 2: else
return top ->data;
9
Prepared by D.HIMAGIRI
Applications of Stack
20 20
15 15 15 15
30 30 30 30 30 30
10 10 10 10 10 10 10 10
Reverse List: 20 15 30 10
10
Prepared by D.HIMAGIRI
Applications of Stack…
2. Parentheses checker :
Stacks can be used to check the validity of parentheses in any algebraic expression.
An algebraic expression is valid if for every open bracket there is a corresponding
closing bracket. Example: { A + ( B – C ) }
Here we scan the expression from left to right,
when open bracket comes push it on to the
stack ,when closed bracket comes pop the
corresponding closed bracket from the stack.
Finally if the stack is empty then parenthesis are (
well balanced other wise not well balanced. { { {
12
Prepared by D.HIMAGIRI
Conversion of an infix expression into a postfix expression...
Example: Infix Expression: A – (B / C + (D % E * F) / G)* H
Step 1: A – (B / C + (D % E * F) / G)* H )
Steps 2-5:
Postfix Expression :A B C / D E F * % G / + H * -
13
Prepared by D.HIMAGIRI
Applications of Stack…
3. Evaluation of a postfix expression
The computer first converts the infix expression into the equivalent postfix notation
and then evaluates the postfix expression.
Both these tasks—converting the infix notation into postfix notation and evaluating
the postfix expression—make extensive use of stacks as the primary tool.
Example:The infix expression 9 – ((3 * 4) + 8) / 4 can be written as 9 3 4 * 8 + 4 / – in
postfix notation.
Algorithm for Evaluation of a postfix expression :
14
Prepared by D.HIMAGIRI
Applications of Stack…
4. Conversion of an infix expression into a prefix expression
Algorithm:
Example:
Infix expression: (A – B / C) * (A / K – L)
Step 1: Reverse the infix string.
while reversing the string we
must interchange left and right
parentheses.
(L – K / A) * (C / B – A)
Step 2: Obtain the corresponding postfix
expression of the infix expression
obtained as a result of Step 1.
(L – K / A) * (C / B – A)
= (L-(K/A))*(C/B-A) = (L-(K/A))*((C/B)-A) = ( (L-( K / A ) )*( ( C/B )-A ) )
Postfix Expression is : L K A / – C B / A – *
Step 3: Reverse the postfix expression to get the prefix expression
Therefore, the prefix expression is: * – A / B C – /A K L
15
Prepared by D.HIMAGIRI
Applications of Stack…
5. Evaluation of a prefix expression
16
Prepared by D.HIMAGIRI
Applications of Stack…
6. Recursion
The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function.
Since a recursive function repeatedly calls itself, it makes use of the system stack to
temporarily store the return address and local variables of the calling function.
Every recursive solution has two major cases. They are
1. Base case: In which the problem is simple enough to be solved directly without
making any further calls to the same function.
2. Recursive case: In which first the problem at hand is divided into simpler sub-parts.
Second the function calls itself but with sub-parts of the problem obtained in the first
step. Third, the result is obtained by combining the solutions of simpler sub-parts.
For the factorial function,
1. Base case is when n = 1, because if n = 1,
the result will be 1 as 1! = 1.
2. Recursive case of the factorial function will
call itself but with a smaller value of n, this
case can be given as Fact(n) = n × Fact (n–1).
17
Prepared by D.HIMAGIRI
Recursion…
18
Prepared by D.HIMAGIRI
Queue
Queue:
A Queue is a linear data structure that works on the principle of FIFO.
The elements can be inserted at the rear end and deleted from the front end.
Queue maintains two variables Front and Rear. Initial value of Front and Rear are -1.
Basic operations performed on queue are:
Insertion-Inserts the element at rear end of the queue .
19
Prepared by D.HIMAGIRI
Queue…
Implementation of queue:
Array Implementation of queue
Linked List Implementation of queue
Array Implementation of queue:
Queues can be represented using linear arrays.
Queue Operations:
Insertion:
This operation is used to insert an element into the queue at rear end. Before
inserting an element we have to check whether the queue is full or not . if
queue is already full it has to display
insertions.
21
Prepared by D.HIMAGIRI
Array Implementation of Queue …
Deletion:
This operation is used to delete an element from front end of the queue.
While deleting an element ,we have to check for underflow condition. An underflow
occurs if front = –1 or front > rear.i,e we cannot delete an element from empty queue.
When an element is deleted, Front is incremented by 1.
Rear value does not change while deleting an element from queue.
Example:
22
Prepared by D.HIMAGIRI
Linked List Implementation of Queue
Insertion:
The new element is added as the last element of the queue.
Initially FRONT=NULL and REAR = NULL.
if FRONT=NULL, then the queue is empty.
There is no overflow condition in linked list implementation of queue.
Example:
23
Prepared by D.HIMAGIRI
Linked List Implementation of Queue
Deletion:
The delete operation is used to delete the element that is first inserted in a queue, i.e.,
the element whose address is stored in FRONT.
Before deleting the value, we must first check if FRONT=NULL because if this is the
case, then the queue is empty and no more deletions can be done.
If an attempt is made to delete a value from a queue that is already empty, an
underflow.
Example:
24
Prepared by D.HIMAGIRI
Types of Queue
Circular Queue:
In linear queues, insertions can be done only at one end called the REAR and deletions
are always done from the other end called the FRONT.
Suppose if we delete first two elements i,e 54 and 9 from the above queue, we get
After deletion, the space cannot be reused for inserting the new elements even though
there is space available, the overflow condition still exists because the condition REAR
= MAX – 1 still holds true. This is a major drawback of a linear queue.
To resolve this problem, we have two solutions.
First, shift the elements to the left so that the vacant space can be occupied and
utilized efficiently. But this can be very time-consuming, especially when the
queue is quite large.
Second, to use a circular queue.
25
Prepared by D.HIMAGIRI
Circular Queue…
In the circular queue, the first index comes right after the last index.
The circular queue will be full only when
FRONT = 0 and REAR = Max – 1.
Operations performed on circular queue are
Insertion
Deletion
Insertion:
For insertion, we now have to check for the following three conditions:
1. If front=0 and Rear=MAX-1, then circular queue is full.
2. If REAR != MAX – 1 , then REAR will be incremented and the value will be
inserted.
26
Prepared by D.HIMAGIRI
Circular Queue…
3. If FRONT != 0 and REAR = MAX – 1, then it means that the queue is not full. So, set
REAR = 0 and insert the new element there.
27
Prepared by D.HIMAGIRI
Circular Queue…
Deletion:
To delete an element, we check for following three conditions:
1. If FRONT = –1, then there are no elements in the queue,
This condition is called as Underflow condition.
3. If the queue is not empty and FRONT = MAX–1, then after deleting the element at
the front, FRONT is set to 0.
28
Prepared by D.HIMAGIRI
Dequeue
Dequeue:
A Dequeue is a list in which the elements can be inserted or deleted at either end.
It is also known as a head-tail linked list because elements can be added to or removed
from either the front (head) or the back (tail) end.
Types of Dequeue:
1. Input Restricted Dequeue : In this dequeue insertions can be done only at
one end but deletions can be done from both the ends.
2. Output Restricted Dequeue : In this dequeue deletions can be done only at
one end but insertions can be done on both the ends.
29
Prepared by D.HIMAGIRI
Queue Applications
Applications of Queue are:
1. Queues are widely used as waiting lists for a single shared resource like printer, disk,
CPU.
2. Queues are used to transfer data asynchronously (data not necessarily received at same
rate as sent) between two processes.
3. Queues are used as buffers on MP3 players and portable CD players, iPod playlist.
4. Queues are used in Playlist for jukebox to add songs to the end, play from the front of
the list.
5. Queues are used in operating system for handling interrupts. When programming a real-
time system that can be interrupted, for example, by a mouse click, it is necessary to
process the interrupts immediately, before proceeding with the current job. If the
interrupts have to be handled in the order of arrival, then a FIFO queue is the appropriate
data structure.
*******
30