Professional Documents
Culture Documents
BLG221E
QUEUES
A1 A2 A3 A4
fron back
t
Operations on queues
Although we can define many operations for a queue, four
are basic: queue, enqueue, dequeue and empty, as defined
below.
A palindrome
A string of characters that reads
the same from left to right as its
does from right to left
| 3 6 9 4 5 | 12 10 19 | 22 29 20 | 34 65 30 81 57 44 99
Implementations of the Queue
Array-based implementations of queue
1. An array-based implementation of queue
2. A circular array-based implementation of queue
0 1 2 3 4 5 6 7
myQueue: 17 23 97 44
front = 0 rear = 3
Front and rear variables hold the index values of the locations.
front = 0 rear = 3
Initial queue: 17 23 97 44
After 17 23 97 44 333
insertion:
front = 1 rear = 4
Problem: Notice how the array contents “crawl”
to the right as elements are inserted and deleted.
This will be a problem after a while!
Circular arrays
We can treat the array (holding the queue elements) as circular
(joined at the ends)
0 1 2 3 4 5 6 7
myQueue: 44 55 11 22 33
rear = 1 front = 5
Elements were added to this queue in the order 11, 22, 33,
44, 55, and will be removed in the same order
Delete: front = (front + 1)
Add: rear = (rear + 1)
Circular arrays
0 1 2 3 4 5 6 7
myQueue: 44 55 11 22 33
rear = 1 front = 5
0 1 2 3 4 5 6 7
enqueue(66) 44 55 66 11 22 33
rear = 2 front = 5
0 1 2 3 4 5 6 7
enqueue(77) 44 55 66 77 11 22 33
rear = 3 front = 5
Circular arrays
0 1 2 3 4 5 6 7
enqueue(77) 44 55 66 77 11 22 33
rear = 3 front = 5
0 1 2 3 4 5 6 7
enqueue(88) 44 55 66 77 88 11 22 33
rear = 4 front = 5
Queues with one element
0 1 2 3 4 5 6 7
myQueue: 88
rear = 4 front = 4
0 1 2 3 4 5 6 7
myQueue:
rear = 4 front = 5
Full and empty queues
If the queue iscompletely full, it would look like this:
0 1 2 3 4 5 6 7
myQueue: 44 55 66 77 88 11 22 33
rear = 4 front = 5
If the queue is completely empty, it would look like this:
0 1 2 3 4 5 6 7
myQueue:
rear = 4 front = 5
So, we need extra mechanism to distinguish
This is a problem! between queue-full and queue-empty
conditions.
Solutions for Queue-Empty/Queue-Full Problem
1. Using a counter to keep the number items in the queue.
• Initialize count to 0 during creation; Increment count by 1 during
insertion; Decrement count by 1 during deletion.
• count=0 empty; count=MAX_QUEUE full
0 1 2 3 4 5 6 7
queue-full
Deleting from a queue
0 1 2 3 4 5 6 7
front = (front+1);
--count;
Full: count == MAX_QUEUE front back
Count=8
Empty: count == 0
Using isFull flag
To initialize the queue, set
front = 0; back = MAX_QUEUE–1; isFull = false;
full queue
Linked-list implementation of queues
In a queue, insertions occur at one end, deletions at the other end
Operations at the front of a singly-linked list are O(1), but at the
other end they are O(n)
Because you have to find the last element each time
fron bac
t k
enqueue(60) enqueue(40)
fron bac fron bac
t k t k
6 6 4
0 0 0
enqueue(50)
fron bac
t k
6 4 5
0 0 0
fron bac
t k
6 4 5
0 0 0
dequeue() dequeue()
fron bac
fron bac
t k
t k
4 5 5
0 0 0
60 40 dequeued
dequeued
dequeue()
fron bac
t k 50
dequeued
Enqueueing a node
Node to
last be
first enqueued
44 97 23 17
last
first
44 97 23 17
front rear
Array implementation of Queue
void delete1() {
if (front == - 1 || front > rear) { 0 1 2 3 4 5 6 7
printf("Queue Underflow \n");
return ;
} rear
front
else
{
printf("Element deleted from queue is Dequeue (delete)
: %d\n", queue_array[front]);
0 1 2 3 4 5 6 7
front = front + 1;
}
} /*End of delete() */ rear
front
void display() {
int i;
if (front == - 1 || front > rear)
printf("Queue is empty \n");
else
0 1 2 3 4 5 6 7
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]); front rear
printf("\n");
} } /*End of display() */
Array implementation of Queue
main() {
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert(); break;
case 2:
delete1(); break;
case 3:
display(); break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}}}
Linked List implemantation of Queues
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
node *ptr;
} *front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
Linked List implemantation of Queues
int main() case 2:
{ deq();
int no, ch, e; break;
case 3:
printf("\n 1 - Enquee"); e = frontelement();
printf("\n 2 - Dequee"); if (e != 0)
printf("\n 3 - Front element"); printf("Front element : %d", e);
printf("\n 4 - Empty"); else
printf("\n 5 - Exit"); printf("\n No front element in Queue as queue is
printf("\n 6 - Display"); empty");
printf("\n 7 - Queue size"); break;
create(); case 4:
empty();
while (1) { break;
printf("\n Enter choice : "); case 5:
scanf("%d", &ch); exit(0);
switch (ch) case 6:
{ display(); break;
case 1: case 7:
printf("Enter data : "); queuesize(); break;
scanf("%d", &no); default:
enq(no); printf("Wrong choice, Please enter correct choice");
break; break;
} } }
Linked List implemantation of Queues
/* Create an empty queue */
void create()
{
front = rear = NULL;
}
front rear
Empty
queue
Linked List implemantation of Queues
front rear
/* Enqueing the queue */
Empty
void enq(int data) queue
{
front rear
if (rear == NULL)
{
rear = new node;
rear->ptr = NULL;
rear->info = data; Inserting first
front = rear; node
} data
else
{
temp=new node;
else
rear->ptr = temp; front rear
temp->info = data;
temp->ptr = NULL; Inserting a
rear = temp;
new node
}
count++;
} data1 data2
New node
Linked List implemantation of Queues
/* Displaying the queue elements */
void display() { front rear
front1 = front;
Empty
if ((front1 == NULL) && (rear == NULL)) { queue
printf("Queue is empty");
return;
}
while (front1 != rear) {
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear) printf("%d", front1->info); }
Queu
e
data data data
Linked List implemantation of Queues
/* Dequeing the queue */ front rear
void deq()
{
front1 = front;
if (front1 == NULL)
{ Empty
printf("\n Error: Trying to display elements queue
from empty queue");
return; rear
}
front
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
delete front; front = front1;
} data
else
{ Deleting first
printf("\n Dequed value : %d", front->info); node
delete front;
front = NULL; rear = NULL;
}
count--; }
Linked List implemantation of Queues