dynamically linked queues
TRANSCRIPT
Dynamically Linked Queues
Steve Paks
Dynamically Linked Queues
• Linked Queue
int MAX_QUEUES 10; /* maximum number of stacks */
Class Queue{
element item;
Queue link;
}
Queue front[MAX_QUEUES], rear[MAX_QUEUES];
Dynamically Linked
Queues(Cont’d)• Add to the rear of a linked queue
void addq(Element item){
Queue temp = new Queue();
if(IS_FULL(temp)){
print(“The memory is full”);
exit(1);
}
temp.item = item;
temp.link = null;
if(front != null) {
rear.link = temp;
} else {
front = temp;
}
rear = temp;
}
∙∙∙∙ (a)
∙∙∙∙ (b)
Dynamically Linked
Queues(Cont’d)• Add to the rear of a linked queue(Cont’d)
front
item
rear(=temp)
(a)
item(b)
front(=rear=temp)
Dynamically Linked
Queues(Cont’d)• delete from the rear of a linked queue
Element deleteq(){
Queue temp = front;
Element item;
if(IS_EMPTY(front)){
print(“The queue is empty?”);
exit(1);
}
item = temp.item;
front = temp.link;
return item;
}
∙∙∙∙ (a)
∙∙∙∙ (b)
∙∙∙∙ (c)
Dynamically Linked
Queues(Cont’d)• delete from the rear of a linked queue(Cont’d)
front(=temp) rear
(a)
(b) a
front rear
item
a
(c) a
front(=temp.link) rear