CS DataStructure-Lecture 3-Queues Arrays and Linked
CS DataStructure-Lecture 3-Queues Arrays and Linked
Input D C B A Output
• Examples of queues
– checkout at supermarket
– Toll Station
• Car comes, pays, leaves
– Check-out in Big super market
• Customer comes, checks out and leaves
– More examples: Printer, Office Hours, …
© Waleed A. Yousef 2008 4
E.g., Printing Queue
n-1 3 2 1 0
D C B A
After A leaves,
n-1 3 2 1 0
D C B
n-1 3 2 1 0
D C B A
Max_Size
rear front
Occupied
Circular Queue
0 Max-1
2 Occupied
Unwinding
Front Rear
Occupied
0 1 … …
Max-1
Linear
© Waleed A. Yousef 2008
Implementation 9
Checking the Boundary conditions
Rear and Front advance in this direction
0 MAX-1
Queue with two elements
F R
F R
Full Condition:
Next to Rear = Front
Therefore we waste one location R F 10
© Waleed A. Yousef 2008
Better solution: Use indicator variable to
distinguish between Empty and Full conditions.
Rear and Front advance in this direction
Empty queue:
Next to Rear = Front.
Size=0 R F
R F
Full queue:
Next to Rear = Front.
Size=MAX R F
front
rear
size
entr
© Waleed A. Yousef 2008
y 14
void CreateQueue(Queue *pq){ User Level
pq->front= 0; (interface)
pq->rear = -1;
void main(){
pq->size = 0;
}
//Initializing front =5 and rear =4 will Queue q;
work if MAXQUEUE >=6. But, since MAXQUEUE
can be 1 we intialize as above. CreateQueue(&q);
MAXQUEUE-1
}
front
&q
rear pq
size
0
entr
© Waleed A. Yousef 2008
y 15
void Append(QueueEntry e, Queue* pq){
pq->rear = (pq->rear + 1) % MAXQUEUE;
pq->entry[pq->rear] = e;
pq->size++;
} if (pq->rear == MAXQUEUE-1)
pq->rear=0;
else
pq->rear++;
MAXQUEUE-1
front
&q
pq rear
size
0
© Waleed A. Yousef 2008
entr 16
void Serve(QueueEntry *pe, Queue* pq){
*pe = pq->entry[pq->front];
pq->front = (pq->front + 1) % MAXQUEUE;
pq->size--;
}
MAXQUEUE-1
front
&q
pq rear
&e size
pe
0
© Waleed A. Yousef 2008
entr 17
int QueueEmpty(Queue* pq){
return !pq->size;
}
MAXQUEUE-1
front
rear
size
0
© Waleed A. Yousef 2008
entr 18
int QueueSize(Queue* pq){
return pq->size;
}
void ClearQueue(Queue* pq){
pq->front = 0;
pq->rear = -1;
pq->size = 0;
}//same as CreateQueue. No nodes to free.
MAXQUEUE-1
front
rear
size
0
© Waleed A. Yousef 2008
entr 19
void TraverseQueue(Queue* pq, void (*pf)(QueueEntry)){
int pos, s;
for(pos=pq->front, s=0; s<pq->size; s++){
(*pf)(pq->entry[pos]);
pos=(pos+1)%MAXQUEUE;
}
}
MAXQUEUE-1
front
rear
size
0
© Waleed A. Yousef 2008
entr 20
Linked Queues(to overcome fixed size limitations):
Just get the idea now, do not worry about details.
q.rear q.front q.rear q.front
Node
q
Empty Queue Queue of size 1
pn
New
node
Last in
q.rear q.Front
pn
}
CreateQueue
&q
pq
q
© Waleed A. Yousef 2008 24
void Append(QueueEntry e, Queue* pq){
QueueNode*pn=(QueueNode*)malloc(sizeof(QueueNode));
pn->next=NULL;
pn->entry=e;
User Call:
Queue q;
QueueEntry e;
pq->rear->next=pn;
pq->rear=pn; Append(e, &q);
e
pq->size++; pn
}
&q
pq e
&q pn
pq
e
Last in
pq->size--;
}
&q
pq pn
&q
pq pn
q
© Waleed A. Yousef 2008 29
void ClearQueue(Queue* pq){
while(pq->front){
pq->rear=pq->front->next;
free(pq->front);
pq->front=pq->rear;
}
pq->size = 0;
}/*Moving with two pointers,
Exactly as in LinkedStacks*/ Last in
&q
pq
Last in
&q
pq
pn
© Waleed A. Yousef 2008 31
Very important note for all linked structures. E.g., in Queues:
In Push and Append we have to check for exhausted memory. The code can be
modified to: