Queue using an array

Post on 31-Dec-2015

33 views 0 download

description

Queue using an array. .head. .tail. Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they point to the same slot, say 0. Add object to rear of list. 1. .head. .tail. Add object to rear of list. 1. 2. .head. .tail. - PowerPoint PPT Presentation

Transcript of Queue using an array

Queue using an array

.head .tail

Pointers head and tail always point to the first empty slot before or afterelements in the list. Thus, initially they point to the same slot, say 0.

.head.tail

Add object to rear of list1

.head.tail

Add object to rear of list1 2

.head.tail

Add object to rear of list1 2 3

.head.tail

Add object to rear of list1 2 3 4

.head .tail

Remove from front

1

2 3 4

.object

.head .tail

Remove from front

2

3 4

.object

.head .tail

Add3 4 5

.head .tail

Remove4 5

3

.object

.head .tail

Add4 5 6

.head .tail

Add4 5 6 7

.head.tail

Add4 5 6 7 8

.tail .head

Add4 5 6 7 89

Queue using Circularly Linked List

.tail

Circularly linked list

.tail

Queue: insert item at rear, remove at front

.tail

Queue: remove from front

_object = tail->next->item;

_object

.tail

Queue: remove from front

_temp = tail->next;

_object

Temp

_temp

.tail

Queue: remove from front

_tail->next = tail->next->next;

_object

Temp

_temp

.tail

Queue: remove from front

_delete temp;

_object

Temp

_temp

.tail

Queue: remove from front

_return object;

_object

Temp

.tail

Queue: remove from front

_

Temp

.tail

Queue: insert at rear

_

Temp

.tail

Queue: insert at rear

_cell = new Cell(object);

Temp

_cell

NULL

.tail

Queue: insert at rear

_cell->next = tail->next;

Temp

_cell

.tail

Queue: insert at rear

_tail->next = cell;

Temp

_cell

.tail

Queue: insert at rear

_tail = tail->next;

Temp

_cell