Documente Academic
Documente Profesional
Documente Cultură
Module 2
Module II (18 hours) Linear Data Structures - Stacks Queues -Lists Dequeues - Linked List - singly, doubly linked and circular lists - Application of linked lists - Polynomial Manipulation - Stack & Queue implementation using Array & Linked List - Typical problems - Conversion of infix to postfix - Evaluation of postfix expression priority queues
10/25/2012
LIST
Flexible data structure: grows and shrinks on demand. Elements can be accessed, inserted, or deleted at any position within
a list.
Lists can be concatenated or split into sub-lists. Mathematically, a list is a sequence of zero or more elements of a
given type. Represented by a comma separated sequence of elements a1,a2,an where n>=0 and each ai is of a given type.
n is the length of the list.
10/25/2012 CS 09 303 Data Structures - Module 2 3
Features Of List
Can be linearly ordered according to their position on the list. Concatenation and splitting possible Main application areas are: => Information retrieval => Programming language translation => Programming language simulation
10/25/2012
LIST OPERATIONS
INSERT(x, p,L) : Inserts x at position p in list L. Move elements at p and following positions to next higher positions. Insert x at position p in list L. If L is a1, a2, , an, then L becomes a1, a2, , ap-1, x, ap, , an . If p is END (L), then L becomes a1, a2, , an, x.
10/25/2012
END(L) : returns the last position of list L LOCATE(x, L) : returns the position of x on list L.
If x appears more than once, it returns the position of first occurrence of x. If x does not appear at all, then it returns END(L).
10/25/2012
NEXT(p, L): returns position following p on list L. PREVIOUS(p, L):returns position preceding p on list L. MAKENULL(L) : Makes the list L empty and returns the position END(L). FIRST(L) : returns first position on list L. PRINTLIST(L) :prints elements of L in order of occurrence.
10/25/2012
Implementation Of List
2 Ways Array Implementation Pointer Implementation(Linked List representation)
10/25/2012
10/25/2012
position = integer;
10/25/2012 CS 09 303 Data Structures - Module 2 10
END(L) Algorithm
function END (var L: List) : position; begin return (L.last + 1) {gives total no. of elements in the list} end; {END}
0 50 1 107 2 15 3 201 L.last
10/25/2012
11
INSERTION Algorithm
procedure INSERT(x : elementtype; p : position; var L: LIST) {INSERT places x at position p in list L} var q:position; begin if L.last >= MAXLEN then error (list is full) else if (p > L.last + 1 ) or ( p < 1) then error(position does not exist) else begin for q := L.last downto p do {shift elements at p, p+1 down by one position} L.elements[q+1] := L.elements[q]; L.last := L.last+1; L.elements[p] := x; end end;{INSERT}
10/25/2012 CS 09 303 Data Structures - Module 2 12
Insertion
Insert (47, 0, L)
0 1 2 3
L.last
CS 09 303 Data Structures - Module 2 13
Insert (59, 1, L)
0 47 L.last p 1 2 3
10/25/2012
14
Insert (65, 2, L)
0 47 1 59 L.last p 2 3
10/25/2012
15
Insert (53, 1, L)
0 47 1 59 p q 2 65 L.last 3
Shift elements down to insert at position 1. Start with q = L.last. L.elements[q+1] := L.elements[q] { Move element at position 2 to position 3. L.elements[2+1] = L.elements[2] L.elements[3] = L.elements[2] }
47 59 p
10/25/2012 CS 09 303 Data Structures - Module 2
65 q L.last q+1
16
L.elements[q+1] := L.elements[q] { Move element at position 1 to position 2. L.elements[1+1] = L.elements[1] L.elements[2] = L.elements[1] }
47 p q
10/25/2012
59 q+1 L.last
65
17
47
53 p q
59
65 L.last
18
10/25/2012
DELETION Algorithm
procedure DELETE (p:position; var L : LIST) {DELETE removes the element at position p of list L} var q:position; begin if ( p > L.last ) or ( p < 1) then error(position does not exist) else begin L.last := L.last-1; for q := p to L.last do {shift elements at p+1, p+2, up one position} L.elements[q] := L.elements[q+1]; end end;{DELETE}
10/25/2012 CS 09 303 Data Structures - Module 2 19
Deletion
Delete (1, L)
0 47 1 53 p 2 59 3 65
L.last
47
53 p
59 L.last
65
10/25/2012
20
To delete element at 1, shift elements at 2 and 3 up by one position. Start with q = p. ie. q = 1.
0 47 1 53 p q q+1 2 59 L.last 3 65
47
59 p q q+1 L.last
65
10/25/2012
21
65 q L.last
22
LOCATE Algorithm
function LOCATE( x:elementtype; var L:LIST) : position; {LOCATE returns position of x on list L} var q:position; begin for q:=1 to L.last do if L.elements[q] = x then return(q); else return( L.last + 1 ) {if not found} end;{LOCATE}
10/25/2012
23
elements are used here. Does not require contiguous memory for storage. Allocates memory dynamically Linked List is made up of cells, and each cell contains: Element of the list Pointer to next cell on the list If the list is a1,a2,an, the cell holding ai has a pointer to the cell holding ai+1, for i = 1, 2, n-1. Header holds no element but points to cell holding a1. The cell holding an has a nil pointer.
CS 09 303 Data Structures - Module 2 25
10/25/2012
10/25/2012
26
10/25/2012
27
10/25/2012
28
10/25/2012
30
10/25/2012
31
There is no work to find the location. Insertion at the beginning of the list
No Worries!
10/25/2012 CS 09 303 Data Structures - Module 2 32
//
No Worries!
10/25/2012 CS 09 303 Data Structures - Module 2 33
//
10/25/2012
34
10/25/2012
35
Explanation
(1) temp := p . next; {address of the cell next to p is stored in temp}
head
4 p
6 Temp
10/25/2012
36
(2) New(p .next); {A new cell is created and its address is assigned to p .next}
head
4 p
p . next
10/25/2012 CS 09 303 Data Structures - Module 2 37
head
4 p
10/25/2012
38
head
4 p temp
head
10/25/2012
40
10/25/2012
41
10/25/2012
42
10/25/2012
43
10/25/2012
44
10/25/2012
45
10/25/2012
46
10/25/2012
47
17
42
Target = 4
10/25/2012
48
head p 6
17
42
10/25/2012
49
head p 6
17
42
Check if p.next.element = 4. NO. So, move onto next cell. p = p.next Target = 4
10/25/2012
50
head
17
42
p Check if p.next.element = 4. YES. So, perform the operation p.next = p.next.next Target = 4
10/25/2012
51
head
17
42
p .next = p.next.next
Target = 4
10/25/2012
52
head
17
42
Target = 4
10/25/2012
53
head
17
42
10/25/2012
54
10/25/2012
56
10/25/2012
57
10/25/2012
58
If Temp < > NIL and i = pos then begin New( newNode); newNode .element : = element; newNode .next : = Temp .next; newNode . previous : = Temp; Temp .next . previous : = newNode; Temp . next := newNode; end { if } Else begin error ( position not found ) end; { INSERT}
10/25/2012 CS 09 303 Data Structures - Module 2 61
Explanation
temp := L; i : =1; temp 5 15 20 head
Check if i<pos, and temp<>NIL. i=1, pos = 2 and temp<>NIL. So, move forward. temp = temp.next i = i+1
10/25/2012 CS 09 303 Data Structures - Module 2 62
head
5 temp
15
20
Check if i<pos, and temp<>NIL. i=2, pos = 2 and temp<>NIL. So, do insertion.
10/25/2012
63
If Temp < > NIL and i = pos then begin new( newNode); {Creates a new node in memory and assigns its address to newnode.}
5 temp
15
20
10/25/2012
64
head
5 temp
15
20
10
10/25/2012
65
head
5 temp
15
20
10
10/25/2012
66
5 temp
15
20
10
10/25/2012
67
head
5 temp
15
20
10/25/2012
68
head
10
15
20
10/25/2012
69
10/25/2012
70
DELETION ALGORITHM Procedure DELETE ( Var p : position); Begin if p . Previous < > nil then { deleted cell not the first } p . Previous . Next : = p . Next; if p . Next < > nil then { deleted cell not the last } p . Next . Previous : = p . Previous End { DELETE}
10/25/2012
71
10/25/2012
72
STACK
An Abstract Data Type Special List : insertions & deletions at one
end(top)
Also called LIFO(Last In First Out) list.
10/25/2012 CS 09 303 Data Structures - Module 2 73
10/25/2012
74
10/25/2012
75
Stack Implementation
Static implementation(Using arrays) Dynamic implementation(Using pointers)
10/25/2012
77
10/25/2012
78
Array Implementation
Procedure PUSH( var x:elementtype; Var s:STACK); Begin If s.top = MAXLENGTH -1 then error(stack is full) Else begin s.top:=s.top+1; s.elements[s.top]:=x; End; End;{PUSH}
10/25/2012 CS 09 303 Data Structures - Module 2 79
Initial stack
3 2 1 0
Top = -1
10/25/2012
80
Push (12, S)
s.top:=s.top+1 = -1 + 1 = 0; s.elements[s.top]:= s.elements[0] = 12; 3 2 1 0 12
Top = 0
10/25/2012
81
Push (34, S)
s.top:=s.top+1 = 0 + 1 = 1; s.elements[s.top]:= s.elements[1] = 34; 3 2 1 0 34 12
Top = 1
10/25/2012
82
Push (47, S)
s.top:=s.top+1 = 1 + 1 = 2; s.elements[s.top]:= s.elements[2] = 47; 3 2 1 0 47 34 12
Top = 2
10/25/2012
83
Push (53, S)
s.top:=s.top+1 = 2 + 1 = 2; s.elements[s.top]:= s.elements[3] = 53; 3 2 1 0 53 47 34 12
Top = 3
10/25/2012
84
Procedure POP( Var s:STACK); begin if EMPTY(s) then error(stack is empty) else s.top := s.top-1; end;{POP}
10/25/2012
85
10/25/2012
86
Function EMPTY( Var s:STACK):boolean; begin If s.top<0 then return(true); else return(false); end;{EMPTY}
10/25/2012
87
Function TOP ( Var s:STACK):elementtype; begin If EMPTY(s) then error(stack is empty) else return(s.elements[s.top]); end;{TOP}
10/25/2012
88
Applications of stack
Explicit and implicit recursive calls Conversion of Infix to postfix Postfix expression evaluation Conversion of infix to prefix Prefix expression evaluation Parenthesis checker
10/25/2012
89
Applications of stack
Expression : operands together with operator Infix notation(A+B) Prefix notation( +AB) Postfix notation( AB+) Operator precedence ^ (Exponential ) .Highest *, /(mul, div) Next highest +, - (add,sub) Lowest
10/25/2012
90
Applications of stack
Conversion from infix to postfix Rules 1. Parenthesize the expression starting from left to right 2. The operands associated with the operator having higher precedence will be parenthesized first. 3. The sub expression which has been converted to postfix is to be treated as single operand. 4. Once the expression is converted to postfix form, remove the parenthesis
10/25/2012
91
(b) Add # to stack. 5. If a left parenthesis is encountered, push it onto the stack. 6. If a right parenthesis is encountered then (a) Repeatedly pop from stack and add to P, until a left parenthesis is encountered. (b) Remove the left parenthesis. [ do not add the left parenthesis to P]. 7. Exit #
10/25/2012
10/25/2012
94
10/25/2012
95
sentinel]. (2) Scan P from left to right and repeat steps 3 and 4 for each element of P until the sentinel ) is encountered. (3) If an operand is encountered, put it on STACK. (4) If an operator # is encountered, then (a) Remove the two top elements of STACK ,where A is the top element and B is the next-to-top element. (b) Evaluate B # A. (c) Place the result onto STACK. 5) RESULT equal to the top element on stack. 6) Exit
10/25/2012 CS 09 303 Data Structures - Module 2 96
Evaluate 45+72-*
10/25/2012
97
Evaluate 59+2*65*+
10/25/2012
98
element of I until the stack is empty. (3) If an operand is encountered, add it to O. (4) If an operator # is encountered, then (a) Repeatedly pop from stack and add to O each operator (on the top of stack) which has higher precedence than #.
10/25/2012 CS 09 303 Data Structures - Module 2 99
(b) Add # to stack. 5. If a right parenthesis is encountered, push it onto the stack. 6. If a left parenthesis is encountered then (a) Repeatedly pop from stack and add to O, until a right parenthesis is encountered. (b) Remove the right parenthesis. [ do not add the right parenthesis to O]. 7. Reverse O to get the prefix expression P. 8. Exit #
10/25/2012
Convert (A*B-F*H)^D
Symbol Scanned D ^ ) H * F B * A ( ( Operator Stack ) )^ )^) )^) )^)* )^)* )^))^))^)-* )^)-* )^ (Empty) Output Expression D D D DH DH DHF DHF* DHF*B DHF*B DHF*BA DHF*BA*DHF*BA*-^
Result: ^-*AB*FHD
10/25/2012 CS 09 303 Data Structures - Module 2 101
Home Work
Convert to prefix: A+ [ B+C - D+E] * F / G +A/*+-+BCDEFG (ab)/c*(d + e f / g) */-abc-+de/fg
10/25/2012
102
Convert (ab)/c*(d + e f / g)
10/25/2012
*/-abc-+de/fg 103
as a sentinel]. (2) Scan P from right to left and repeat steps 3 and 4 for each element of P until the sentinel ( is encountered. (3) If an operand is encountered, push it onto STACK. (4) If an operator # is encountered, then (a) Remove the two top elements of STACK ,where A is the top element and B is the next-to-top element. (b) Evaluate A # B. (c) Place the result onto STACK. 5) RESULT equal to the top element on stack. 6) Exit
10/25/2012 CS 09 303 Data Structures - Module 2 104
Evaluate *+45-72
10/25/2012
105
Parenthesis Checker
To check whether a mathematical expression is properly parenthesized or not. 3 sets of grouping symbols The standard parenthesis () The braces { } The brackets []
10/25/2012
106
10/25/2012
107
10/25/2012
108
else pop a symbol s from the stack if (s does not correspond to c ) then print error incorrect nesting of symbols exit end {if} end {if} end {if} end {for}
if (stack is not empty ) then print error missing closing symbol else print input expression is OK end {if} END
10/25/2012
110
Self-Study (Homework)
Stack using Linked List Write algorithms to:
Push an element onto stack Pop an element Create an empty stack Check if stack is empty Return the top element from the stack
10/25/2012
111
QUEUES
Abstract data type which is special kind of List where items are
inserted at one end (rear) and deleted from the other end (front).
Insertion :Rear Deletion :Front Also called FIFO(First In First Out) list.
10/25/2012 CS 09 303 Data Structures - Module 2 112
front
rear
10/25/2012
113
QUEUE OPERATIONS
MAKENULL(Q) : makes queue Q an empty list FRONT(Q) : returns first element of Q ENQUEUE(x, Q) : inserts element x at end of Q DEQUEUE(Q) : deletes first element of Q EMPTY(Q) : returns true if Q is an empty queue.
10/25/2012
114
10/25/2012
115
10/25/2012
116
IMPLEMENTATION OF QUEUE
Using Arrays (Static implementation) Using Pointers (Dynamic implementation)
10/25/2012
117
Array Implementation
Type QUEUE = record elements : array[1..MAXLENGTH] of elementtype; front, rear: integer; end;
10/25/2012
118
10/25/2012
119
(1)Empty Queue
Rear := -1 Front := -1 0 rear front 1 2 3
(2)ENQUEUE(10,Q)
Front := 0 Rear := rear + 1 = 0 Q[Rear] := Q[0] = 10
10
0 rear 1 front 2 3
10
0 front 1
3
2 3 rear
120
10/25/2012
(4)ENQUEUE(17,Q)
Rear := rear + 1 = 1 + 1 =2 Q[Rear] := Q[2] = 17
10
0 1 front
17
2 rear 3
(5)ENQUEUE(29,Q)
Rear := rear + 1 = 2+1 = 3 Q[Rear] := Q[3] = 29
10
0 front 1
17
2
29
3 rear
10/25/2012
121
Deletion Operation Function DEQUEUE(Var Q: QUEUE) : elementtype; Begin if Q.rear < Q.front then begin Q.front:=0; Q.rear:= -1; error(Queue is empty); exit(); end else begin dequeue := Q.elements[Q.front]; Q.front:= Q.front+1; end; end;{DEQUEUE}
(2)
10/25/2012
122
(1)DEQUEUE(Q)
data := Q[front] = Q[0] =10 front := front + 1 = 0 + 1 = 1 0 1 front
17
2
29
3 rear
(1)DEQUEUE(Q)
data := Q[front] = Q[1] = 3 front := front + 1 = 1 + 1 = 2
17
0 1 front 2
29
3 rear
(2)DEQUEUE(Q)
data := Q[front] = Q[2] = 17 front := front + 1 = 2 + 1 = 3
29
0 1 2 3 front rear
10/25/2012
123
(4)DEQUEUE(Q)
data := Q[front] = Q[3] = 29 front := front + 1 = 3 + 1 = 4 0 1 2 3 rear front = 4
10/25/2012
124
type celltype = record element: elementtype; next : celltype; end; type QUEUE = record front, rear : celltype; end;
10/25/2012 CS 09 303 Data Structures - Module 2 125
(1) To Make a NULL QUEUE procedure MAKENULL(Var Q:QUEUE); begin new(Q.front);{create header cell } Q.front .next := nil; Q.rear := Q.front; {header is both first &last cell } end;{MAKENULL}
10/25/2012
126
(2) Check if QUEUE is empty function EMPTY(Var Q:QUEUE): boolean; begin if Q.front = Q.rear then return(true); else return(false); end;{EMPTY}
10/25/2012
127
function FRONT(Var Q:QUEUE):elementtype begin If EMPTY(Q) then error(Queue is empty); else return(Q . front . next . element) end;{FRONT}
10/25/2012
128
Procedure ENQUEUE( var x:elementtype;Var Q:Queue); begin new(Q.rear . next); {add new cell to rear of queue} Q.rear := Q.rear . next; Q. rear .element := x; Q.rear .next:=nil; end; {ENQUEUE}
10/25/2012
129
Function DEQUEUE(Var Q:Queue): elementtype; Begin if EMPTY(Q) then error(queue is empty) else begin dequeue := Q.front .element; Q.front := Q.front .next; end; end; {DEQUEUE}
10/25/2012 CS 09 303 Data Structures - Module 2 130
10/25/2012
131
Circular Queue
10/25/2012
132
CIRCULAR QUEUE
Q[1] following Q[n] Conditions of circular Queue: Insertion Condition
rear = (rear+1) % SIZE
Deletion Condition
front = (front + 1) % SIZE
QUEUE FULL
rear = front
10/25/2012 CS 09 303 Data Structures - Module 2 133
Rear=-1 Front=-1
10/25/2012
135
0
Front = 0
1 14
3
10/25/2012 CS 09 303 Data Structures - Module 2
2
136
0
Front = 0
Rear = 1
3
10/25/2012 CS 09 303 Data Structures - Module 2
2
137
0
Front = 0
20 3
10/25/2012 CS 09 303 Data Structures - Module 2
Rear = 2
138
1 14 42
Queue full
20 2
CS 09 303 Data Structures - Module 2 139
37
Rear = 3
10/25/2012
1 14 42
37
Rear = 3
10/25/2012
20 2
3
CS 09 303 Data Structures - Module 2
140
Deletion Function DEQUEUE(Var Q : QUEUE): elementtype; begin if Q.front = -1 then begin error(Queue is empty) exit(); end else dequeue := Q.elements[Q.front]; Q.front := (Q.front + 1) % SIZE; end;{DEQUEUE}
10/25/2012
141
10/25/2012
142
Operations on Dequeue
(1) Add an element at the rear end ( insert right) (2)Delete an element from the front end ( delete left ) (3) Add an element at the front end ( insert left ) (4) Delete an element from the rear end (delete right )
10/25/2012
144
if Q.left = -1 then begin Q.left := 0; Q.right := 0; end else begin if Q.right = MAX-1 then Q.right := 0; else Q.right := Q.right+1; end; Q.elements[Q.right] := data; end; {insertRightDequeue}
10/25/2012
146
l r
10/25/2012
147
10/25/2012
148
10/25/2012
149
insertRightDequeue ( 10, Q) Increment right. Right := right + 1 = 1 + 1 = 2; Insert at right. Q.elements[right] : = Q.elements[2] = 10;
0 5 left = 0 1 2 2 10 right = 2 3
10/25/2012
150
insertRightDequeue ( 17, Q) Increment right. Right := right + 1 = 2 + 1 = 3; Insert at right. Q.elements[right] : = Q.elements[3] = 17;
0 5 left = 0 1 2 2 10 3 17 right = 3
10/25/2012
151
Perform a delete operation. So, left will be incremented by one. left := left + 1 = 0 + 1 = 1;
1 2 left = 1
2 10
3 17 right = 3
10/25/2012
152
insertRightDequeue (15, Q) 15 can be inserted at 0. If left <> -1 and right = MAX 1, there are vacant positions at the beginning of the queue. Set right = 0 and insert at 0.
0 15 right = 0 1 2 left = 1 2 10 3 17
10/25/2012
153
10/25/2012
154
if Q.left = -1 then begin Q.left := 0; Q.right := 0; end else begin if Q.left = 0 then Q.left := MAX-1; else Q.left := Q.left-1; end; Q.elements[Q.left] := data; end; {insertLeftDequeue}
10/25/2012 CS 09 303 Data Structures - Module 2 155
l r
10/25/2012
156
10/25/2012
157
insertLeftDequeue ( 2, Q) If left = 0, set left = MAX - 1. left := MAX - 1 = 4 - 1 = 3; Insert at left. Q.elements[left] : = Q.elements[3] = 2;
0 5 right = 0 1 2 3 2 left = MAX- 1
10/25/2012
158
insertLeftDequeue ( 10, Q) Decrement left. left := left - 1 = 3 - 1 = 2; Insert at left. Q.elements[left] : = Q.elements[2] = 10;
0 5 right = 0 1 2 10 left = 2 3 2
10/25/2012
159
insertLeftDequeue ( 17, Q) Decrement left. left := left - 1 = 2 - 1 = 1; Insert at left. Q.elements[left] : = Q.elements[1] = 17;
0 5 right = 0 1 17 left = 1 2 10 3 2
10/25/2012
160
Perform a delete operation. Right will be set to MAX 1 so that element at zero is deleted first. right := MAX - 1 = 4 - 1 = 3;
1 17 left = 1
2 10
3 2 right = 3
10/25/2012
161
insertLeftDequeue (15, Q) 15 can be inserted at 0. If left <> -1 and left <> 0, there are vacant positions at the beginning of the queue. Set left := left - 1 = 2 -1 = 0 and insert at 0.
0 15 left = 0 1 2 2 10 3 17 right = 3
10/25/2012
162
10/25/2012
163
if (Q.left = Q.right ) then begin {only one element} Q.left:= -1; Q.right := -1; end;{if} if (Q.right = 0) then Q.right = MAX -1; else Q.right = Q.right -1; end; { deleteRightDequeue}
10/25/2012
165
deleteRightDequeue ( Q) Right = 0. So, delete the first element. data := Q.elements[right] = Q.elements[0] = 5; Set Right := MAX - 1 = 3; {Next deletion occurs at right = 3}.
0 5 right = 0 1 2 left = 1 2 10 3 17
10/25/2012
166
10/25/2012
167
10/25/2012
168
deleteRightDequeue (Q) data : = Q.elements[right] : = Q.elements[1] = 5; Left = right = 1; {only one element} Set left := -1, right := -1; {Queue becomes empty}
0 1 2 left = 1 right = 1 2 3
10/25/2012
169
deleteRightDequeue (Q) Empty Queue (left = right = -1) Cannot perform deletion
l r
10/25/2012
170
if Q.left = Q.right then begin {only one element} Q.left:= -1; Q.right := -1; end;{if} if ( Q.left = MAX - 1 ) then Q.left = 0; else Q.left = Q.left + 1; end; { deleteLeftDequeue}
10/25/2012
172
10/25/2012
173
10/25/2012
174
10/25/2012
175
deleteLeftDequeue (Q) data : = Q.elements[left] : = Q.elements[3] = 18; Left = right = 3; {only one element} Set left := -1, right := -1; {Queue becomes empty}
0 1 2 3 18 left = 3 right = 3
10/25/2012
176
deleteLeftDequeue (Q) Empty Queue (left = right = -1) Cannot perform deletion
l r
10/25/2012
177
Example
Insert right (rear)
0 21 left = 0 right = 0
10/25/2012
178
0 21 right = 0
3 42 left = 3
10/25/2012
179
0 21
1 35 right = 1
3 42 left = 3
10/25/2012
180
0 21
1 35 right = 1
2 5 left = 2
3 42
10/25/2012
181
0 21
1 35 right = 1
3 42 left = 2
10/25/2012
182
0 21 right = 0
3 42 left = 2
10/25/2012
183
3 42 left = 2 right = 3
10/25/2012
184
l ,r = -1
10/25/2012
185
PRIORITY QUEUE
Collection of elements such that each element is assigned a priority number. Rules are: An element of higher priority is processed before any element of lower priority. Elements of same priority are processed according to the order in which they were added into the queue.
10/25/2012
186
10/25/2012
187
Implementation
Implementation of priority queue: (1) One way list representation (2) Multiple Queues (one for each priority - Array representation) (3) Maximum or minimum heap
10/25/2012
188
10/25/2012
189
Operations performed: Searching an element with maximum priority Inserting an element Deleting an element with maximum priority
10/25/2012
190
Start
A 2 NULL
10/25/2012
191
Start
B 1
A 2 NULL
10/25/2012
192
Start
B 1
A 2
C 2 NULL
10/25/2012
193
Start
B 1
A 2
C 2 NULL
10/25/2012
194
Start
A 2
C 2 NULL
10/25/2012
195
of the list.
10/25/2012
196
first node }
(2) Delete first node from the list (3) Process ITEM (4) Exit
10/25/2012
197
Array Implementation
As multiple queues. Multiple queues are defined in the same array queue[] of size n. Each queue i is allocated a predefined space bounded by array indices. A queue is maintained for each priority. In order to process an element of the priority queue, element from non-empty highest priority number queue is accessed.
10/25/2012 CS 09 303 Data Structures - Module 2 198
front[0]
rear[0] front[1]
rear[1] front[2]
rear[2] front[3]
rear[3]
Queue1
Queue2
Queue3
Queue4
10/25/2012
199
10/25/2012
200
10/25/2012
201
10/25/2012
202
Applications of Queue
Round robin techniques :Process scheduling Print server routines All types of customer service software(Railway/airticket reservation)
10/25/2012
203
10/25/2012
204
10/25/2012
205
p = 2x3 + 3xy + y2 + yz Store the polynomial such that terms are arranged in descending order of powers of x first, then y and then z.
3002 1103 0201 0111
10/25/2012
206
10 x + 11 x +
10/25/2012
207
5 6 1570
ptrp
0 4 NULL 1750
1526
4 2 1660
ptrq
1500
1826
10/25/2012
208
3 2 1607
ptrp
0 4 NULL 1750
1570
1 10 1700 1660
1500
4 2 Null 1870
10/25/2012
209
3 2 1607
ptrp
0 4 NULL 1750
1570
1 10 1700
ptrq
1660
3 2 Null 1860
10/25/2012
210
1 1 1750
ptrp
0 4 NULL 1750
1607
0 1 NULL 1700
1660
p.powerx = q.powerx (Add the coefficients and increment both the pointers)
5 6 1870 1826 4 2 1860 1870 3 2 1807 1860
ptrr
1 11 Null 1807
10/25/2012
211
0 4 NULL
ptrp
1750
1700
NULL 1950
212
ptrr
10/25/2012
213
Else if the power of first polynomial > power of the second polynomial then
Insert the term from first polynomial into sum polynomial. Obtain the next term in the first polynomial.
Else
Insert the term from second polynomial into sum polynomial. Obtain the next term in the second polynomial. 4. Copy remaining terms from non-empty polynomial into sum polynomial. 5. Return the address of the sum polynomial.
10/25/2012 CS 09 303 Data Structures - Module 2 214
Thank You
10/25/2012
215