Data Structure Unit 2 PPT
Data Structure Unit 2 PPT
Engineering
U20EST356
DATA STRUCTURES
Handling by
Mr.J.Muruganandham
Assistant Professor
Department of EEE
UNIT II
STACK AND QUEUE OPERATIONS
Stacks and Queues: ADT Stack and its
operations. Applications of Stacks: Expression Conversion
and evaluation. ADT Queue and its operations. Types of
Queue: Simple Queue – Circular Queue – Priority Queue –
Deque.
What is Linear Data Structure
In linear data structure, data is arranged in
linear sequence.
Data items can be traversed in a single run.
In linear data structure elements are accessed
or placed in contiguous(together in sequence)
memory location.
STACK
• A Stack is a linear data structure which follows Last In
First Out (LIFO) principle,
• which both insertion and deletion occur at only one
end of the list called the Top.
• Example : Pile of coins, a stack of trays in cafeteria
Basic Operations on the STACK
isfull()
Algorithm of isfull()
function -
begin procedure isfull
if top equals to MAXSIZE-1
return 1
else
return 0
end if
end procedure
isempty()
els
e
3. top=top
+1 end
if
ALGORITHM OF DELETION IN
STACK: (POP)
1. Deletion(a,top,item)
2. If t o p =- 1 then
print ‘STACK UNDERFLOW’
exit
else
3. item=a[top]
end if
4. t o p = t o p - 1
5. Exit
ALGORITHM OF DISPLAY IN
STACK:
1.Display(top,i,a[i])
2.If t op=0 then
Print ‘STACK EMPTY’
Exit
Else
3.For
i=t op
to 0
Print a[i]
End for
4.exit
For example:
To reverse the string ‘REVERSE’ the string
is read from left to right and its characters
are pushed . LIKE:
onto a stack.
APPLICATIONS OF STACKS ARE:
I. Reversing Strings:
• Examples:
3+4*5 (3 + (4 * 5) ) 345*+
a/b^c–d*e–a*c^3^4 abc^/de*ac34^^*--
• Examples:
3+4*5 (3 + (4 * 5) ) +3*4 5
( (a / (b ^ c)) – ( (d * e) – (a * (c ^ (3 ^ 4) ) ) ) )
Application of Stacks - Evaluating Postfix Expression (Cont’d)
Pop the only operand from the stack: this is the result of the evaluation
Application of Stacks - Evaluating Postfix Expression (Cont’d)
• Example: Consider the postfix expression, 2 10 + 9 6 - /, which is (2 + 10) / (9 -
6) in infix, the result of which is 12 / 3 = 4.
• The following is a trace of the postfix evaluation algorithm for the postfix expression:
Using a Stack to Process Algebraic
Expressions
postfixVect
Infix to postfix conversion
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix to postfix conversion
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix to postfix conversion
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix to postfix conversion
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix to postfix conversion
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix to postfix conversion
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix to postfix conversion
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix to postfix conversion
stackVect
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix to postfix conversion
stackVect
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix to postfix
stackVect conversion
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Application of Stacks – Infix to Prefix Conversion
An infix to prefix conversion algorithm:
1. Reverse the infix string
2. Perform the infix to postfix algorithm on the reversed string
3. Reverse the output postfix string
Example: (A + B) * (B – C)
reverse
(C – B) * (B + A)
C B - B A + *
reverse
* + A B - B C
QUEUE
Queue
Algorithm
begin procedure peek
return queue[front]
end procedure
isfull()
end procedure
isempty()
Q[0] F=R=Q[0]
Q[5]
Q[4]
Q[1]
Q[3] Q[2]
Operations on Circular Queue
Insertion:
Algorithm:
5 Q[0] F=Q[0]
Q[5] 30
10
25
Q[4] Q[1]
20 15
Q[3] Q[2]
Queue is full(Overflow)
e.g.: (cond..)
Q[6] Q[7]
Q[4]
Q[1]
Q[3] Q[2]
Q[5] Q[6]
35 40
R=Q[7]
45 Q[7]
Q[4] 30
Q[3] 25 Q[0]
20 15
Q[2] Q[1] F=Q[1]
Q[6] Q[7]
10
Q[4]
Q[1] R=Q[1]
Q[3] Q[2]
Rear=Rear+1
Operations on Circular Queue
Deletion:
Algorithm:
Q[6] Q[7]
Q[4]
Q[1]
Q[3] Q[2]
Queue is Empty(Underflow)
e.g.:
Q[6] Q[7]
Q[4]
Q[1]
Q[3] Q[2]
Q[5] Q[6]
35 40
R=Q[7]
45 Q[7]
Q[4] 30
Q[3] 25 Q[0]
20 15
Q[2] Q[1] F=Q[1]
Q[7] Q[8]
35 40
R=Q[1]
45 Q[1]
Q[6] 30
Q[5] 25 Q[2]
20
Front=Front+1
Priority Queue
Priority Queue
• In priority queue, each element is assigned a
priority.
• Priority of an element determines the order in
which the elements will be processed.
• Rules:
1. An element with higher priority will processed
before an element with a lower priority.
2. Two elements with the same priority are
processed on a First Come First Serve basis.
Types of Priority Queue
1. Ascending Priority Queue
In this type of priority queue, elements can be inserted
into any order but only the smallest element can be
removed.
Front Rear
20 15 10
A[0] A[1] A[2] A[3] A[4]
Front Rear
25 15 13 10
A[0] A[1] A[2] A[3] A[4]
Front Rear
Array Representation of Priority
Queue
Deletion Operation:
•While deletion, the element at the
front is always deleted.
Array Representation of Priority
Queue
20 15 13 10
A[0] A[1] A[2] A[3] A[4]
Front Rear
15 13 10
A[0] A[1] A[2] A[3] A[4]
Front Rear
13 10
A[0] A[1] A[2] A[3] A[4]
Front Rear