Professional Documents
Culture Documents
Queues
QUEUES
• Operations on Queues
Queues
ARRAY IMPLEMENTATION OF QUEUES
int queue[n];
Queues
ARRAY IMPLEMENTATION OF QUEUES
queue[9]
queue[8] 1
queue[7] 3 3
queue[6] 7 7 7
queue[5] 5 5 5 5
queue[4] 9 9 9 9 9
queue[3] 8 8 8 8 8 8
queue[2] 6 6 6 6 6 6 6
queue[1] 2 2 2 2 2 2 2 2
queue[0] 4 4 4 4 4 4 4 4 4
Queues
ARRAY IMPLEMENTATION OF QUEUES
queue[9]
queue[8] 1 1 1 1 1 1
queue[7] 3 3 3 3 3 3 The program should
queue[6] 7 7 7 7 7 7
therefore keep
track of which cell
queue[5] 5 5 5 5 5 5 holds the element
queue[4] 9 9 9 9 9 at the front of the
queue[3] 8 8 8 8 queue and which
cell holds the
queue[2] 6 6 6 element at the rear.
queue[1] 2 2
queue[0] 4
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
queue[9]
queue[8] 1 rear = 8 1 rear = 8 1 rear = 8 1 rear = 8 rear = 8 front = 8
queue[7] 3 3 3 front = 7
queue[6] 7 7 front = 6
queue[5] 5 front = 5
queue[4] front = 4
queue[3] To delete the
queue[2] front most
item, simply
queue[1] increment
queue[0] front.
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
• How to determine if the queue is full?
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
queue[9] 9 9 9 9
• If rear = n - 1 (the
queue[8] 1 1 1 1 1
highest index)
queue[7] 3 3 3 3 3 and an item is to
queue[6] 7 7 7 7 7 be added, then
that item is sent
queue[5] 5 5 5 5 5
to the low index
queue[4] end of the array.
queue[3]
queue[2] 8 • The queue
queue[1] 6 6 therefore crawls
in a circular space
queue[0] 4 4 4
Queues
ARRAY IMPLEMENTATION OF QUEUES
queue[9] 9 9 front = 9
queue[8] 5 front = 8
queue[7] front = 7
queue[6]
queue[5]
queue[4]
queue[3]
queue[2]
queue[1] 2 rear = 1 2 rear = 1 2 rear = 1 2 rear = 1 rear = 1 front = 1
queue[0] 4 4 4 front = 0
Take note that the queue is still empty when front = rear.
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
queue[9] 9 9 9 9 9
queue[8] 5 5 5 5 5
queue[7] front = 7 front = 7 front = 7 front = 7 10 front = 7 rear = 7
queue[6] 6 rear = 6 6
queue[5] 8 rear = 5 8 8
queue[4] 3 rear = 4 3 3 3
queue[3] 1 rear = 3 1 1 1 1
queue[2] 7 7 7 7 7
queue[1] 2 2 2 2 2
queue[0] 4 4 4 4 4
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
ARRAY IMPLEMENTATION OF QUEUES
Queues
OPERATIONS ON QUEUES
int queue[MAX_SIZE];
int front, rear;
Queues
OPERATIONS ON QUEUES
Queues
OPERATIONS ON QUEUES
int
int queue_empty (int int front, int rear)
rear)
{
if (front == rear)
return (1);
else
return (0);
}
Queues
OPERATIONS ON QUEUES
Queues
OPERATIONS ON QUEUES
• Function to Remove an Item from the Queue (and return the value of the item)
Queues
OPERATIONS ON QUEUES
int
int front_item (int
int queue[ ], int front,
front, int
int rear)
rear)
{
if (queue_empty(front, rear) == 1)
cout << "\nThe Queue is Empty.";
else
if (front == MAX_SIZE - 1)
return (queue[0]);
else
return (queue[front+1]);
}
Queues
LINKED LIST IMPLEMENTATION OF QUEUES
rear
• If front and rear are both equal to null, then the queue is said to
be empty. There is no such condition as a full queue in using
linked lists.
Queues
LINKED LIST IMPLEMENTATION OF QUEUES
rear
struct NODE
{
int value;
NODE *next;
};
Queues
LINKED LIST IMPLEMENTATION OF QUEUES
• The algorithm for inserting an item into the queue (assume the
pointer ptr will point to the new node and the variable item
contains the value of this node). The first step is to create a node
for the new item:
rear
ptr 555
Queues
LINKED LIST IMPLEMENTATION OF QUEUES
• The next step is to attach the node at the rear of the queue:
rear
rear
ptr 555
Queues
LINKED LIST IMPLEMENTATION OF QUEUES
• However, if the queue is empty (front and rear are both equal to
NULL), then front and rear should be made to point to the new
node:
Queues
LINKED LIST IMPLEMENTATION OF QUEUES
• The following is the algorithm for removing an item from the queue:
free(ptr);
}
Queues
OPERATIONS ON QUEUES
struct NODE
{
int value;
NODE *next;
};
Queues
OPERATIONS ON QUEUES
Queues
OPERATIONS ON QUEUES
if (queue_empty(*ptr_front, *ptr_rear) == 1)
{
*ptr_front = ptr;
*ptr_rear = ptr;
}
Queues
OPERATIONS ON QUEUES
else
{
(*ptr_rear) -> next = ptr;
*ptr_rear = ptr;
}
Queues
OPERATIONS ON QUEUES
if (queue_empty(*ptr_front, *ptr_rear)==1)
cout << "\nQueue is Empty!";
else
{
item = (*ptr_front)-> value;
ptr = *ptr_front;
*ptr_front = (*ptr_front)->next;
Queues
OPERATIONS ON QUEUES
if (*ptr_front == NULL)
*ptr_rear = NULL;
free(ptr);
Queues
OPERATIONS ON QUEUES
Queues