0% found this document useful (0 votes)
32 views

Data Structure Unit 2 PPT

Uploaded by

kkyoto24
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Data Structure Unit 2 PPT

Uploaded by

kkyoto24
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 77

Department of Electrical and Electronics

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

• push() − Pushing (storing) an element on the


stack.
• pop() − Removing (accessing) an element from
the stack.
• peek() − get the top data element of the stack,
without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.
isfull()

isfull()
Algorithm of isfull()
function -
begin procedure isfull
if top equals to MAXSIZE-1
return 1
else
return 0
end if
end procedure
isempty()

Algorithm of isempty() function -


begin procedure isempty
if top less equal to -1
return 1
else
return 0
endif
end procedure
ALGORITHM OF INSERTION IN
STACK: (PUSH)
1. Push(int x)
2. If top=max then
print ‘STACK
OVERFLOW’
exit

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:

• A simple application of stack is reversing strings.


To reverse a string , the characters of string are pushed
onto the stack one by one as the string is read from left
to right.

• Once all the characters


of string are pushed onto stack, they are
popped one by one. Since the character last pushed
in comes out first, subsequent pop operation results
in the reversal of the string.
Infix to Postfix conversion
Infix to Postfix conversion (manual)
• An Infix to Postfix manual conversion algorithm is:
1. Completely parenthesize the infix expression according to order of
priority you want.
2. Move each operator to its corresponding right parenthesis.
3. Remove all parentheses.

• Examples:
3+4*5 (3 + (4 * 5) ) 345*+

a/b^c–d*e–a*c^3^4 abc^/de*ac34^^*--

((a / (b ^ c)) – ((d * e) – (a * (c ^ (3 ^ 4) ) ) ) )


Infix to Prefix conversion (manual)
• An Infix to Prefix manual conversion algorithm is:
1. Completely parenthesize the infix expression according to order of
priority you want.
2. Move each operator to its corresponding left parenthesis.
3. Remove all parentheses.

• Examples:
3+4*5 (3 + (4 * 5) ) +3*4 5

a/b^c–d*e–a*c^3^4 -/a^b c-* d e*a^c^3 4

( (a / (b ^ c)) – ( (d * e) – (a * (c ^ (3 ^ 4) ) ) ) )
Application of Stacks - Evaluating Postfix Expression (Cont’d)

• The following algorithm uses a stack to evaluate a postfix expressions.

Start with an empty stack


for (each item in the expression) {
if (the item is an operand)
Push the operand onto the stack
else if (the item is an operator operatorX){
Pop operand1 from the stack
Pop operand2 from the stack
result = operand2 operatorX operand1
Push the result onto the stack
}
}

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

• Algebraic expressions composed of


• Operands (variables, constants)
• Operators (+, -, /, *, ^)
• Operators can be unary or binary
• Different precedence notations
• Infix a + b
• Prefix + a b
• Postfix a b +
Infix to postfix conversion
Use a loop to read the tokens one by one from a vector infixVect of
tokens (strings) representing an infix expression.
For each token do the following in the loop:
• When the token is an operand
• Add it to the end of the vector postfixVect of token (strings) that is
used to store the corresponding postfix expression

• When the token is a left parenthesis “(”


• Push_back the token x to the end of the vector stackVect of token
(strings) that simulates a stack.
• When the token is a right parenthesis “)”
• Repeatedly pop_back a token y from stackVect and push_back
that token y to postfixVect until “(“ is encountered in stackVect.
Then pop_back “(“ from stackVect.
• If stackVect is already empty before finding a “(“, that expression
is not a valid expression.
• When the token is an operator, see next slide.
Infix to postfix conversion
• When the token x is an operator
• Write a loop that checks the following three
conditions:
1. The stack stackVect is not empty
2. The token y currently in the end of stackVect is an operator.
In other words, it is not not a lef parenthesis “(“ .
3. y is an operator of higer or equal precedence than that of x,
• As long as the three conditions above are all true,
in the loop above do the following in the body of
the loop :
– push_back the token y to postfixVect
– pop_back the token y from stackVect
• The loop above will stops as soon as any of the
three conditions is not true.
• After the loop, push_back the token x into
stackVect.
Infix to postfix conversion
After the loop in slide 1 has processes all the tokens in infixVect and stop,
use another loop to repeatedly do the following as long as the stack
vector stackVect is not empty yet:
• push_back the token on the top of the stack vector stackVect into
postfixVect.
• pop_back the stack vector to remove the top token y the stack.
Infix to postfix
conversion
infixVect
(a+b-c)*d–(e+f)

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)

Infix to postfix algorithm

C B - B A + *

reverse

* + A B - B C
QUEUE
Queue

 Queue is an abstract data structure, somewhat similar to


Stacks.
 Unlike stacks, a queue is open at both its ends. One end
is always used to insert data (enqueue) and the other is
used to remove data (dequeue).
 Queue follows First-In-First-Out methodology, i.e., the
data item stored first will be accessed first
BASIC OPERATIONS

• enqueue() - add (store) an item to the queue.

• dequeue() - remove (access) an item from the


queue.

• peek() - Gets the element at the front of the


queue without removing it.

• isfull() - Checks if the queue is full.

• isempty() - Checks if the queue is empty.


peek( )
This function helps to see the data at the front of
the queue. The algorithm of peek() function is
as follows –

Algorithm
begin procedure peek
return queue[front]
end procedure
isfull()

As we are using single dimension array to implement queue, we


just check for the rear pointer to reach at MAXSIZE-1 to determine
that the queue is full.

Algorithm of isfull() function –

begin procedure isfull


if rear equals to MAXSIZE-1
return 1
else
return 0
endif

end procedure
isempty()

Algorithm of isempty() function –

begin procedure isempty


if front equal to -1 OR front is greater than rear
return 1
else
return 0
endif
end procedure
Enqueue Operation
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error
and exit.
• Step 3 − If the queue is not full, increment rear pointer
to point the next empty space.
• Step 4 − Add data element to the queue location,
where the rear is pointing.
• Step 5 − return success.
Circular Queue
Why Circular Queue is needed?
• Problem:
– Wastage of memory in standard queue in DEQUEUE
operation
What is Circular Queue?
• The Arrangement of the elements Q[0], Q[1],

• ...,Q[n] in a circular fashion with Q[1]


following Q[n] is called Circular Queue.

• The last node is connected to first node


to make a circle.

• Initially, Both Front and Rear pointers


points to the beginning of the array.

• It is also known as “Ring Buffer”.


• e.g.:
Q[6] Q[7]

Q[0] F=R=Q[0]
Q[5]

Q[4]
Q[1]

Q[3] Q[2]
Operations on Circular Queue
Insertion:
Algorithm:

Step 1: IF FRONT=0 and REAR=MAX-1 Then


Write “Overflow”
Goto Step 4
[END OF IF]
Step 2: IF FRONT = REAR=-1 then
SET FRONT=REAR=0 ELSE IF REAR=MAX-1 and FRONT!=0
SET REAR=0
ELSE
SET REAR=REAR+1
[END OF IF]
Step 3: SET QUEUE[REAR]=VAL
Step 4: EXIT
e.g.:

Q[6] Q[7] R=Q[7]


35 40

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[5] 2 Q[0] F=R=Q[0]

Q[4]
Q[1]

Q[3] Q[2]

If F=R=-1 then F=R=0


e.g.: (cond..)

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]

If REAR=MAX-1 and FRONT!=0


e.g.: (cond..)

Q[6] Q[7]

Q[5] 5 Q[0] F=Q[0]

10
Q[4]
Q[1] R=Q[1]

Q[3] Q[2]

Rear=Rear+1
Operations on Circular Queue
Deletion:
Algorithm:

Step 1: If FRONT = -1 then


Write ("Circular Queue Underflow“)
GOTO Step 4 Step 2: SET VAL=QUEUE[FRONT]
Step 3: If FRONT = REAR then
SET FRONT=REAR=-1
ELSE
IF FRONT=MAX-1
SET FRONT=0
ELSE
SET FRONT=FRONT+1 [END OF IF]
[END OF IF] Step 4: EXIT
e.g.:

Q[6] Q[7]

Q[5] Q[0] F=R=-1

Q[4]
Q[1]

Q[3] Q[2]

Queue is Empty(Underflow)
e.g.:

Q[6] Q[7]

Q[5] 5 Q[0] F=R=Q[0]

Q[4]
Q[1]

Q[3] Q[2]

If F=R=0 then F=R=-1


e.g.: (cond..)

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]

If Front=MAX-1 then Front=0


e.g.: (cond..)

Q[7] Q[8]
35 40

R=Q[1]
45 Q[1]
Q[6] 30

Q[5] 25 Q[2]

20

F=Q[4] Q[4] Q[3]

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.

2. Descending Priority Queue


In this type of priority queue, elements can be inserted
into any order but only the largest element can be
removed.
Array Representation of Priority
Queue
Insertion Operation:
•While inserting elements in priority queue we
will add it at the appropriate position
depending on its priority
•It is inserted in such a way that the elements
are always ordered either in Ascending or
descending sequence
Array Representation of Priority
Queue
15 10
A[0] A[1] A[2] A[3] A[4]

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

You might also like