Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Unit-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Prepared by D.

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

PEEK-Returns top most element


Stack maintains a variable called TOP,
if TOP=-1 then the stack is empty.
1
Prepared by D.HIMAGIRI
Stack…

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--.

Stack can be implemented in two ways:


I. Stack Implementation using Arrays
II. Stack Implementation using Linked List

Stack Implementation using Arrays:


Stack can be represented using array as

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

 Stack Implementation using Linked List:


In a linked stack, every node has two parts—one that stores data and another that stores
the address of the next node.
 if the stack size cannot be determined in advance, then linked representation is used.

The START pointer of the linked list is used as TOP.

All insertions and deletions are done at the node pointed by TOP.

If TOP = NULL, then it indicates that the stack is empty.

The linked representation of a stack is :

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….

Operations on a Linked Stack :

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

similarly Push 2,4,3,7,1 we get

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.

Now the new top element is 7.

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

The applications of the stack are


1. Reversing a list
A list of numbers can be reversed by reading each number from an array starting from
the first index and pushing it on a stack. Once all the numbers have been read, the
numbers can be popped one at a time and then stored in the array starting from the first
index.
Example: 10,30,15,20
Push: Pop:

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. { { {

3. Conversion of an infix expression into a postfix expression


 Expression: It is collection of operands and operator which can be evaluated to
single value.
 Different types of Expressions:
1. Infix Expression-operators is in between their operands. Ex:5+2
2. Prefix Expression-operator is followed by its operands. Ex:+52
3. Postfix Expression- operands are followed by its operator. Ex: 52+
11
Prepared by D.HIMAGIRI
Conversion of an infix expression into a postfix expression...
Example:
Infix: 5*4/2+7-8 Prefix: - + / * 5 4 2 7 8 Postfix: 5 4 * 2 / 7+ 8 -
= (5*4)/2+7-8
= ((5*4)/2) +7-8
= ( ( ( ( 5*4 ) /2 ) +7 )-8 )

 Algorithm to convert an infix expression into a postfix expression:

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

Algorithm: Example: Prefix: + - 2 7 * 8 / 4 12

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.

 It is opened at both the ends (Rear and Front Ends).

 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 .

 Deletion- deletes the element from front 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

queue overflow otherwise it permits

insertions.

 While inserting the first element both rear

and front are set to zero, from second

insertion onwards only rear will be incremented by 1.


20
Prepared by D.HIMAGIRI
Array Implementation of Queue …
Insertion Example:

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.

2. If the queue is not empty and FRONT = REAR, then


after deleting the element at the front the queue
becomes empty and so FRONT and REAR are set to –1.

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

You might also like