DS-Unit2
DS-Unit2
Syllabus
Stack ADT
Operations
Applications
Infix to Postfix Conversion
Evaluating arithmetic expressions
Tower of Hanoi
Queue ADT-Operations
Circular queue implementation
DeQueue
Applications of queues
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Course Outcome
Introduction
Key points to be remembered
Data structure
Types
Liner Vs. Non-linear
Examples
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Example
Array
Linked list
Stack
Queue
Stack
Key points to be remembered
Stack
Basic operations
Scenario
Stack applications
Implementation
Issues with Array implementation
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Key points to be remembered
It is a linear data structure in which
insertions & deletions are made at one end
called “top”
Stack
Key points to be remembered
LIFO (Last In First Out)
Pushdown list
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Operations involved
2 basic operations
1. Push
2. pop
Stack
Operations involved
1. Push
It is the operation used to insert the data
element at the top of the stack
2. Pop
It is the operation used to delete the data
element from the top of the stack
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Operations involved
Assume a stack is implemented using an
array and the data items 10, 20 & 30 are
inserted in sequence. This can be pictured as
30 20 10
30 top
20 top
10 top
top
Stack
Key points to be remembered
Stack can be referred as “dynamic structure”
coz’ of its frequent insertion & deletion
operations
i.e., during
Push – size of the stack increases
Pop – size of the stack shrinks
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Key points to be remembered
How does a stack changes?
With reference to the definition of stack,
the data items may be inserted at the top
of the stack in that case stack moves
upward to the highest element.
Stack
Primitive operations
Basically there are 2 operations that can be
performed on top of the stack namely push &
pop.
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Primitive operations
Empty(s) – This function is used to say
whether a stack is empty or not. It results
Boolean value as either true or false
Stack
Applications of Stack
System Stack
Evaluation of an Expression – 3 types
Evaluation of Infix expression
Evaluation of Postfix expression (Reverse
Polish)
Evaluation of Prefix expression (Polish)
Towers of Hanoi Puzzle
Checking Balanced Parenthesis from the
given expression
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Key points to be remembered: Implementation
Can be done by 2 ways
1. Array Implementation
2. Linked List implementation (Pointer)
Stack
Implementation: Issues with an Array
These conditions will occur if a stack is
implemented using an array
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Implementation: Stack Representation
Stack is a ordered collection of items, when
ever a problem solution calls for the use of
stack, it is generally implemented by an
array, where array is also a collection of data
items.
Stack
Implementation: Stack Representation
Array can not be a stack but its a home of a
stack. i.e., Array can be used to manipulate
the stack operation where array is been
declared with large enough size of the stack.
10
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Implementation: Stack Representation
Assume the problem is implemented using
an Array.
Stack
Implementation: Stack Representation
It can be represented as
#define size 10
class stack{
int a[size];
int top;
public:
stack(){
top=-1;
}
void push(int);
int pop();
};
11
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Implementation: Stack Representation
Implementing PUSH operation
Stack
Implementation: Stack Representation
Implementing PUSH operation Ex.: 10,20,30
10
20 30
top
30
top
20 20
top
top 10 10 10
Empty Stack Top=0
Top=1
Top = -1
Top=3
12
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack
Implementation: Stack Representation – pop()
void pop(){
if(s.top==0){
cout<<“\n\dStack is Empty !”);
} else{
item=s.a[top];
s.top--;
}
}
Stack
Implementation: Stack Representation – pop()
30 20 10
top
30
top
20 20
top
10 10 10 top
Empty Stack Empty Stack
Empty Stack Empty Stack
Top=1 Top = 0
Top=3 Top=2
13
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Key points to be remembered
It is a linear data structure in which
insertions takes place at one end called
“rear” & deletions takes place at other end
called “front”
Queue
Queue Operations
Insertion (enqueue)
Deletion (dequeue)
Empty
14
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Key points to be remembered
Assume a Queue is implemented using an
array with the data items A, B & C. Queue can
be represented as:
rear
A B C
front
Queue
Queue Operations
Insertion
It is the operation that is used to insert a
data item at the “rear” end of a Queue
Deletion
It is the operation which is used to remove
the data item from the “front” end of
Queue
Empty
This function is used to assess the empty
state of the queue. It returns either true or
false
15
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Queue Application
Job Scheduling Techniques
Note
With reference to the queue definition, there
is no upper limit specified. It says about the
maximum number of data elements that
queue can hold
Queue
Implementation
Can be done by 2 ways
Arrays
Pointers
Note
While representing Queue using array or
linked list, it needs two variables namely
“rear” and “front” to hold the position of last
and first element of Queue
16
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Implementation
Issues with an Array
These conditions will occur if a Queue is
implemented using an array
Queue
Implementation: Queue Representation
Assume the problem is implemented using
an Array.
17
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Implementation: Queue Representation
It can be represented as
#define size 10
class queue{
int a[size];
int rear, front;
public:
queue(){
rear=-1; front=0;
}
void enqueue(int);
int dequeue();
};
Queue
Implementation: Queue Representation
Implementing Insert operation
18
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Implementation: Queue Representation
Implementing Delete operation
void dequeue(){
if(rear<front || front ==-1){
cout<<“Queue is Empty !”;
} else{
int rem;
rem=a[front];
front++;
cout<<“Data is removed”;
}
}
Queue
Example
Assume you want to insert data items 10, 20,
30 & 40 to a Queue. The schematic
representation of Queue during insertion
operation can be pictured as:
Array 10 20
10
r f f r
19
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
40
Example
Array 30 10 20 30
10 20
f r
f r front=0 front=0
rear=1 rear=2
50
front=0
f rear=3 r front=0
rear=4
Queue
Deletion
Assume you have the data items 10, 20, 30 &
40 stored in a Queue. The schematic
representation of Queue during deletion
10 operation can be pictured as:
Array
20
20 30 40
30 40
30
f r
f r 40
fr
20
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Stack and Queue
Laboratory
class definition
class definition
enqueue
push
dequeue
pop
display
display
empty
empty
full
full
21
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Applications of Stack
Key terms to be remembered
System Stack
Evaluation of an Expression – 3 types
Evaluation of Infix expression
Evaluation of Postfix expression (Reverse
Polish)
Evaluation of Prefix expression (Polish)
Towers of Hanoi Puzzle
Checking Balanced Parenthesis from the
given expression
System Stack
Key terms to be remembered
It is used by a program at run time to
process function calls
22
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
System Stack
Key terms to be remembered
Since only one function can be executed at
any given time, the function which is in the
top of the stack is chosen.
System Stack
Key terms to be remembered
And previous stack frame has a pointer that
points to the invoking function
23
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
System Stack
Key terms to be remembered
For ex: consider a program. Assume a main
function invokes a function “m1”
The size of stack before & after the
invoking of function “m1” can be pictured
as:
fp
Prev. frame pointer
Return address m1
Local variables
fp
Prev. frame pointer Prev. frame pointer
Return address main Return address main
Figure 1 Figure 2
Expression Evaluations
Key terms to be remembered
For evaluating expressions, Stack is a
suitable data structure.
What is an expression?
24
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Key terms to be remembered
Evaluation of Infix expression
Evaluation of Postfix expression
Evaluation of Prefix expression
Remember !!!
Expression Evaluations
Infix Expression
It is a usual algebraic expression / notation
where a arithmetic operator appears between
2 operands.
25
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Operator Precedence
It says order of execution of an expression
Precedence
Operator Operation Associativity
level
[] Array element reference Left to right
1
() Function invocation Right to left
++ Increment
- Unary minus
-- Decrement Left to right 2
! Logical negation
~ One’s complement
Expression Evaluations
Operator Precedence
It says order of execution of an expression
Precedence
Operator Operation Associativity
level
% Modulus
* Multiplication 3
/ Division
+ Addition
Left to right 4
- Subtraction
>> Right shift
<< Left shift 5
>>> Right shift with zero fill
26
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Operator Precedence
It says order of execution of an expression
Precedence
Operator Operation Associativity
level
> Greater than
6
>= Greater than or equal to
Expression Evaluations
Infix Expression
This expression may require parenthesis to
specify a desired order of operations
Example:
consider an expression “A/B+C”. In this
expression, division will occur first
27
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Postfix Expression (Reverse Polish)
Here, the need of parenthesis is eliminated
because the operator is placed directly after
the operands.
Example
An expression “A/B+C” can be written as
“AB/C+” in postfix form
Expression Evaluations
Prefix Expression (Polish)
Here, the need of parenthesis is eliminated
because the operator is placed directly
before the operands.
Example
An expression “A/B+C” can be written as
“+/ABC” in prefix form
28
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Expression Evaluations
Evaluation of Postfix Expression
Has 2 steps
Convert the given infix expression into
postfix expression
Evaluate the postfix expression
29
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Infix to Postfix conversion process
Completely parenthesize the given infix
expression to specify the order of all
operations
Expression Evaluations
Infix to Postfix conversion process
To parenthesis an expression, there is a
level of precedence
Level Operator Symbol
Highest Exponentiation ^
Next highest Multiplication *
Division /
Lowest Addition +
Substraction -
30
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Infix to Postfix conversion process
For instance, consider an example of
expression
A/B^C+D*E-A*C
Expression Evaluations
Infix to Postfix conversion process
Moving each operator to its corresponding
right parenthesis then we obtain:
( ( (A / (B ^ C) ) + (D * E) ) – (A * C) )
31
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Infix to Postfix conversion process
Example
1) 4.99 * 1.06 + 5.99 + 6.99 * 1.06
2) A / B ^ C + D * E - A * C
3) ( ( A * B ) + ( C - ( D / E ) ) )
4) A + B * C + ( D * E + F ) * G
5) A + (B * C - ( D / E î F ) * G ) * H
Expression Evaluations
Infix to Postfix conversion process (Algorithm)
1. Scan the expression from left to right
2. If a left parenthesis “(” is encountered, push
it onto the stack
3. If a operand is encountered, write it to the
output
4. If an operator is encountered, push it onto
the stack
5. If a right parenthesis is encountered, then
repeatedly POP from the stack & add it to the
output until a corresponding left parenthesis
is encountered – remove the left parenthesis
(do not add in the output)
32
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Infix to Postfix conversion process
Example:
4.99 * 1.06 + 5.99 + 6.99 * 1.06
Expression Evaluations
Evaluation of Postfix Expression
Computing the result of the given Postfix
expression
33
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Evaluation of Postfix Expression (Algorithm)
1. Read / scan the Postfix expression from left
to right
2. If an element is an operand, push that
element onto the stack
3. If an element is an operator:
a) POP 2 operands from the stack (POP one
operand in case of NOT operator)
b) Evaluate the expression formed by 2
operands & the operator
c) PUSH the result of the expression onto the
stack
Expression Evaluations
Evaluation of Postfix Expression (Algorithm)
4. If no more elements POP the result else
continue the process from step 1.
34
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Evaluation of Postfix Expression
1. Evaluate the postfix expression “AB+C*D/” if
A=2,B=3, C=4 and D=5 starting from left to
right
Expression Evaluations
Solution AB+C*D/
1. First element is an operand “A”, PUSH “A”
into stack
TOP
B
A
35
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Solution
3. Next element is an operator “+” then POP 2
elements from the stack i.e., B & A and
evaluate the expression
POP B POP A
TOP
TOP
B
TOP
A A
Stack is empty
Evaluate the expression after POP then push the result onto the stack
Expression Evaluations
Solution
Evaluating = A + B
=2+3
=5
4. PUSH the result onto the stack
PUSH 5
TOP
TOP
5
Stack is empty
36
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Solution
5. Next element is an operand “C”, PUSH “C”
into the stack
TOP
TOP
C
5 5
Expression Evaluations
Solution
6. Next element is an operator “*” then POP 2
elements from the stack i.e., C & 5 and
evaluate the expression
POP C POP 5
TOP
TOP
C TOP
5 5
Stack is empty
37
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Solution
Evaluating = 5 * C
= 5 * 4 (C = 4)
= 20
7. PUSH the result onto the stack
PUSH 20
TOP
TOP
20
Stack is empty
Expression Evaluations
Solution
8. Next element is an operand “D”, PUSH “D”
into the stack
TOP
TOP
D
20 20
38
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Solution
9. Next element is an operator “/” then POP 2
elements from the stack i.e., D & 20 and
evaluate the expression
POP D POP 20
TOP
TOP
D TOP
20 20
Stack is empty
Evaluate the expression after POP then push the result onto the stack
Expression Evaluations
Solution
Evaluating = 20 / D
= 20 / 5 (D = 5)
=4
10. PUSH the result onto the stack
PUSH 4
TOP
TOP
4
Stack is empty
39
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Solution
11. No more elements in the input of expression
so POP the element & display it as a result
POP 4
TOP
Expression Evaluations
Evaluation of Postfix Expression
2. Evaluate the postfix expression “6 5 2 3 +
8 * + 3 + *” starting from left to right
DO IT YOURSELF?
40
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Infix to Prefix conversion process (Algorithm)
1. Scan the expression from right to left
2. If a right parenthesis “)” is encountered,
push it onto the stack
3. If a operand is encountered, write it to the
output
4. If an operator is encountered, push it onto
the stack
5. If a left parenthesis is encountered, then
repeatedly POP from the stack & add it to the
output until a right parenthesis is
encountered – remove the right parenthesis
(do not add in the output)
Expression Evaluations
Infix to Prefix conversion process
Example:
4.99 * 1.06 + 5.99 + 6.99 * 1.06
41
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Infix to Prefix conversion process
Example:
A $ B * C - D + E / F / ( G + G)
DO IT YOURSELF?
Expression Evaluations
Evaluation of Prefix Expression
Computing the result of the given Prefix
expression
42
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
1. Read / scan the Postfix expression from right
to left
2. If an element is an operand, push that
element onto the stack
3. If an element is an operator:
a) POP 2 operands from the stack (POP one
operand in case of NOT operator)
b) Evaluate the expression formed by 2
operands & the operator
c) PUSH the result of the expression onto the
stack
4. If no more elements POP the result else
continue the process from step 1.
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
1. Evaluate the prefix expression “+A*B+CD” if
A=2,B=3, C=4 and D=5 starting from right to
left
43
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
+A*B+CD
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
1. First element is an operand “D”, PUSH “D”
into stack
TOP
C
D
+A*B+CD
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
3. Next element is an operator “+” then POP 2
elements from the stack i.e., B & A and
evaluate the expression
POP C POP D
TOP
TOP
C TOP
D D
Stack is empty
Evaluate the expression after POP then push the result onto the stack
44
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
Solution:
Evaluating = C + D
= 4 + 5 [C = 4; D = 5]
=9
4. PUSH the result onto the stack
PUSH 9
TOP
TOP
9
Stack is empty
TOP
TOP
B
9 9
45
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
6. Next element is an operator “*” then POP 2
elements from the stack i.e., B & 9 and
evaluate the expression
POP B POP 9
TOP
TOP
B TOP
9 9
Stack is empty
Evaluate the expression after POP then push the result onto the stack
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
Evaluating = 9 * B
= 9 * 3 (B = 3)
= 27
7. PUSH the result onto the stack
PUSH 27
TOP
TOP
27
Stack is empty
46
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
+A*B+CD
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
8. Next element is an operand “A”, PUSH “A”
into the stack
TOP
TOP
A
27 27
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
9. Next element is an operator “+” then POP 2
elements from the stack i.e., A & 27 and
evaluate the expression
POP A POP 27
TOP
TOP
A TOP
27 27
Stack is empty
Evaluate the expression after POP then push the result onto the stack
47
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
Evaluating = 27 + A
= 27 + 2 (A = 2)
= 29
10. PUSH the result onto the stack
PUSH 29
TOP
TOP
29
Stack is empty
Expression Evaluations
Evaluation of Prefix Expression (Algorithm)
11. No more elements in the input of expression
so POP the element & display it as a result
POP 4
TOP
29
48
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Expression Evaluations
Evaluate prefix expression
Example
1) 4.99 * 1.06 + 5.99 + 6.99 * 1.06
2) A / B ^ C + D * E - A * C
49
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Checking Balanced Parenthesis
Algorithm
1. Scan the given expression from left to right
50
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Checking Balanced Parenthesis
Problems
Consider the following expressions, check
whether the given expressions are balanced
or not?
1. {p+(q-[m+n])*j-[(x+y)]}
2. ( p + ( q – ( m + n ) ) * j – ( ( x + y ) ) ) / ( j – k - ( - k ( 1 –n ) ) )
“{ p + ( q - [ m + n ] ) * j - [ ( x + y ) ] }”
51
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Checking Balanced Parenthesis
{p+(q-[m+n])*j-[(x+y)]}
Solution
PUSH { PUSH (
TOP
TOP
TOP (
{ TOP {
Stack is empty
PUSH [
TOP
[
(
{
POP [
TOP
(
{
52
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Checking Balanced Parenthesis
{p+(q-[m+n])*j-[(x+y)]}
Solution
Next symbol is “)”, then POP its
corresponding “(” symbol where it matches
POP (
TOP
PUSH (
PUSH [
TOP
TOP
(
[ [
{ {
53
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Checking Balanced Parenthesis
{p+(q-[m+n])*j-[(x+y)]}
Solution
Next symbol is “)”, then POP its
corresponding “(” symbol where it matches
POP (
TOP
[
{
POP [
TOP
54
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Checking Balanced Parenthesis
Solution
Next symbol is “}”, then POP its
corresponding “{” symbol where it matches
POP {
TOP
( p + ( q – ( m + n ) ) * j – ( ( x + y ) ) ) / ( j – k - ( - k ( 1 –n ) ) )
DO IT YOURSELF?
55
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Key terms to be remembered
Queue
Queue Types
Queue
Key points to be remembered
It is a linear data structure in which
insertions takes place at one end called
“rear” & deletions takes place at other end
called “front”
56
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue
Key terms to be remembered
3 types
1. Circular Queue
2. Deque
3. Priority Queue
Circular Queue
Key terms to be remembered
In this Queue,
First element is stored immediately after
the last element
57
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Circular Queue
Key terms to be remembered
Front end of the queue always points to the
first element of the queue
Circular Queue
Key terms to be remembered
front
Q[0] Q[0]
Q[5] Q[5]
10
30
Q[3] Q[2] Q[3] Q[2]
58
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Circular Queue
Key terms to be remembered
To perform the insertion, the position of the
element to be inserted can be calculated by
the relation
value = queue[front]
front = (front + 1) % maxsize
Circular Queue
Routine: Insertion
59
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Circular Queue
Routine: Deletion
void delete(){
if(front==rear){
printf(“Queue is empty”);
}
else{
front = (front + 1) % maxsize
item=q[front];
}
}
Dequeue
Key terms to be remembered
Referred as “Double Ended Queue”
60
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Dequeue
2 variations of Dequeue
1. Input restricted deque
2. Output restricted deque
Dequeue
Key terms to be remembered
1. Input restricted deque
It is a “deque” which allows insertion at
only one end of the list but allows
deletions at both ends of the list
61
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Priority Queue
Key terms to be remembered
Prehistory
In stack and Queue, the date elements are
inserted in the sequence order, where by
deletion on Stack / Queue concentrated on
any one of its ends.
Priority Queue
Key terms to be remembered
Synonym
It is a data structure in which elements are
stored in natural order or intrinsic order
Example:
Arrangement of student list based on rank
or age
2 Types
1. Ascending PQ
2. Descending PQ
62
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Ascending Priority Queue
Key terms to be remembered
It is a collection of data items into which
items can be inserted arbitrarily and from
which only smallest item can be removed
63
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Priority Queue
Implementation
Insertion and deletion operations of both
stack and queue involves in accessing single
element of the array. This is not possible in
Priority Queue
Priority Queue
Implementation
When there is a attempt to remove an
element from a APQ, there are 2 issues must
be considered: Use pqmindelete(apq)
1. To locate smallest element, every element
of an array must be examined. So, deletion
operation requires accessing of all the
elements in PQ
2. How an element situated at middle of array
can be removed?
Stack and Queue do not require searching,
where as PQ requires searching for the
element to be deleted and remove the
element in the middle of array
64
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Priority Queue
Implementation
After a data item is deleted, an “empty
indicator” can be placed in that position to
indicate the empty state
Priority Queue
Implementation
Shifting of all elements?
After the deletion is made, all other
elements are shifted to its prior position.
Insertion becomes unchanged. But
deletion becomes inefficient
65
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Queue Applications
Key points to be remembered
“Queues” are used for any situation where
you want to efficiently maintain a First-in-first
out order on some entities.
66
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]