You are on page 1of 31

Lovely Professional University, Punjab

Data Structures
Lecture: Queue
Outlines
 Introduction
 Examples of Queues
 Application of Queues
 Basic Operations on Queue
 Representation of Queues
 Array Representation
 Linked Representation
Introduction
 A Queue is a linear list of elements in which deletions can take
place only at one end, called the ‘FRONT’ and insertions can
take place only at the other end, called the ‘REAR’.

 Queues are also called as FIFO list.


Array
Representation
Insertion into a Queue
Q_INSERT (QUEUE, N, FRONT, REAR, ITEM)

1. If FRONT = 1 and REAR = N, or If FRONT = REAR+1, then:


Write: OVERFLOW and Return. [Overflow]
2. If FRONT = NULL, then [Queue initially empty]
Set FRONT = 1 and REAR = 1.
Else if REAR = N, then:
Set REAR = 1.
Else:
Set REAR = REAR + 1.
[End of if Structure.]
3. Set Queue[REAR] = ITEM.
4. Return.
Deletion in a Queue
Q_DELETE (QUEUE, N, FRONT, REAR, ITEM)

1. If FRONT = NULL, then:


Write: UNDERFLOW and Return. [Underflow]
2. Set ITEM = QUEUE [FRONT].

3. If FRONT = REAR, then [Queue has only one element]


Set FRONT = NULL and REAR = NULL.

Else if FRONT = N, then:


Set FRONT = 1.
Else:
Set FRONT = FRONT + 1.
[End of if Structure.]
4. Return.
// Queue implementation in C void enQueue(int value) {
if (rear == SIZE - 1)
#include <stdio.h> printf("\nQueue is Full!!");
#define SIZE 5 else {
if (front == -1)
void enQueue(int); front = 0;
void deQueue(); rear++;
void display(); items[rear] = value;
printf("\nInserted -> %d", value);
int items[SIZE], front = -1, rear = -1; }
}
int main() { void deQueue() {
//deQueue is not possible on empty queue if (front == -1)
deQueue(); printf("\nQueue is Empty!!");
else {
//enQueue 5 elements printf("\nDeleted : %d", items[front]);
enQueue(1); front++;
enQueue(2); if (front > rear)
enQueue(3); front = rear = -1;
enQueue(4); }
enQueue(5); }
// Function to print the queue
// 6th element can't be added to because the queue is full void display() {
enQueue(6); if (rear == -1)
printf("\nQueue is Empty!!!");
display(); else {
int i;
//deQueue removes element entered first i.e. 1 printf("\nQueue elements are:\n");
deQueue(); for (i = front; i <= rear; i++)
printf("%d ", items[i]);
//Now we have just 4 elements }
display(); printf("\n");
}
return 0;
}
LINKED
Representation
Insertion into a Queue
LINK_Q_INSERT (INFO, LINK, FRONT, REAR, AVAIL, ITEM)

1. [Available space?] If AVAIL = NULL, then:


Write “OVERFLOW” and Exit.
2. Set NEW = AVAIL, and AVAIL = LINK [AVAIL].

3. Set INFO [NEW] = ITEM and LINK [NEW] = NULL.

4. If FRONT = NULL, then: FRONT = REAR = NEW.


Else: Set LINK [REAR] = NEW and REAR = NEW

5. Exit
Deletion in a Queue
LINK_Q_DELETE (INFO, LINK, FRONT, REAR, AVAIL, ITEM)

1. [Queue Empty?] If FRONT = NULL, then:


Write “UNDERFLOW” and Exit.
2. Set TEMP = FRONT.

3. Set ITEM = INFO [TEMP].

4. If FRONT = REAR then


5. Set FRONT = REAR = NULL
6. Else
7. Set FRONT = LINK [FRONT]

8. Set LINK [TEMP] = AVAIL and AVAIL = TEMP.

9. Exit
#include<stdio.h> void dequeue()
#include<stdlib.h> {
//used to free the first node after dequeue
struct node struct node *temp;
{
int data;
if(front == NULL)
struct node *next;
}; printf("Queue is Empty. Unable to perform
dequeue\n");
struct node *front = NULL, *rear = NULL; else
{
void enqueue(int val) //take backup
{ temp = front;
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL; //make the front node points to the next node
//logically removing the front element
//if it is the first node front = front->next;
if(front == NULL && rear == NULL)
//make both front and rear points to the new node //if front == NULL, set rear = NULL
front = rear = newNode; if(front == NULL)
else
rear = NULL;
{
//add newnode in rear->next
rear->next = newNode; //free the first node
free(temp);
//make the new node as the rear node }
rear = newNode;
} }
}
int main()
void printList()
{
{
enqueue(10);
struct node *temp = front;
enqueue(20);
enqueue(30);
while(temp)
printf("Queue :");
{
printList();
printf("%d->",temp->data);
dequeue();
temp = temp->next;
printf("After dequeue the new Queue :");
}
printList();
printf("NULL\n");
dequeue();
}
printf("After dequeue the new Queue :");
printList();

return 0;
}
Deques
 Deque (Double-ended queue) is a linear list in which elements
can be added or removed at either end but not in the middle.

 Two variations of Deque are:


1. Input-restricted deque
2. Output-restricted deque
Variations of Deque
Input-Restricted Deque:
allows insertion only at one end while deletion in both ends of
the list.

Output-Restricted Deque:
allows deletion only at one end while insertion at both the ends
of the list.
// Deque implementation in C printf("\nElements in a deque: ");
#include <stdio.h> display(arr);
#define MAX 10
i = delFront(arr, &front, &rear);
void addFront(int *, int, int *, int *);
void addRear(int *, int, int *, int *); printf("\nremoved item: %d", i);
int delFront(int *, int *, int *);
int delRear(int *, int *, int *); printf("\nElements in a deque after deletion: ");
void display(int *); display(arr);
int count(int *);
addRear(arr, 16, &front, &rear);
int main() { addRear(arr, 7, &front, &rear);
int arr[MAX];
int front, rear, i, n; printf("\nElements in a deque after addition: ");
front = rear = -1; display(arr);

for (i = 0; i < MAX; i++) i = delRear(arr, &front, &rear);


arr[i] = 0; printf("\nremoved item: %d", i);

addRear(arr, 5, &front, &rear); printf("\nElements in a deque after deletion: ");


addFront(arr, 12, &front, &rear); display(arr);
addRear(arr, 11, &front, &rear);
addFront(arr, 5, &front, &rear); n = count(arr);
addRear(arr, 6, &front, &rear); printf("\nTotal number of elements in deque:
addFront(arr, 8, &front, &rear); %d", n);
}
void addFront(int *arr, int item, int void addRear(int *arr, int item, int *pfront, int
*pfront, int *prear) { *prear) {
int i, k, c; int i, k;
if (*pfront == 0 && *prear == MAX - 1) {
printf("\nDeque is full.\n"); if (*pfront == 0 && *prear == MAX - 1) {
printf("\nDeque is full.\n");
return; return;
} }
if (*pfront == -1) {
*pfront = *prear = 0; if (*pfront == -1) {
arr[*pfront] = item; *prear = *pfront = 0;
return; arr[*prear] = item;
return;
} }
if (*prear != MAX - 1) {
c = count(arr); if (*prear == MAX - 1) {
k = *prear + 1; k = *pfront - 1;
for (i = 1; i <= c; i++) { for (i = *pfront - 1; i < *prear; i++) {
arr[k] = arr[k - 1]; k = i;
if (k == MAX - 1)
k--; arr[k] = 0;
} else
arr[k] = item; arr[k] = arr[i + 1];
*pfront = k; }
(*prear)++; (*prear)--;
} else { (*pfront)--;
}
(*pfront)--; (*prear)++;
arr[*pfront] = item; arr[*prear] = item;
} }
}
int delFront(int *arr, int *pfront, int *prear) { void display(int *arr) {
int item; int i;
int delFront(int *arr, int *pfront, int *prear) {
int item; printf("\n front: ");
for (i = 0; i < MAX; i++)
if (*pfront == -1) { printf(" %d", arr[i]);
printf("\nDeque is empty.\n"); printf(" :rear");
return 0; }
}
int count(int *arr) {
item = arr[*pfront]; int c = 0, i;
arr[*pfront] = 0;
for (i = 0; i < MAX; i++) {
if (*pfront == *prear) if (arr[i] != 0)
*pfront = *prear = -1; c++;
else }
(*pfront)++; return c;
}
return item;
} if (*pfront == -1) {
printf("\nDeque is empty.\n");
int delRear(int *arr, int *pfront, int *prear) { return 0;
int item; }

if (*pfront == -1) { item = arr[*pfront];


printf("\nDeque is empty.\n"); arr[*pfront] = 0;
return 0;
} if (*pfront == *prear)
*pfront = *prear = -1;
item = arr[*prear]; else
arr[*prear] = 0; (*pfront)++;
(*prear)--;
if (*prear == -1) return item;
*pfront = -1; }
return item;
}
int delRear(int *arr, int *pfront, int *prear) {
int item;

if (*pfront == -1) {
printf("\nDeque is empty.\n");
return 0;
}

item = arr[*prear];
arr[*prear] = 0;
(*prear)--;
if (*prear == -1)
*pfront = -1;
return item;
}

void display(int *arr) {


int i;

printf("\n front: ");


for (i = 0; i < MAX; i++)
printf(" %d", arr[i]);
printf(" :rear");
}

int count(int *arr) {


int c = 0, i;

for (i = 0; i < MAX; i++) {


if (arr[i] != 0)
c++;
}
return c;
}
Priority Queue
 A Priority Queue is a collection of elements such that each
element has been assigned a priority and such that the order in
which elements are processed comes from the following rules:

o An element of higher priority is processed before any element of


lower priority.
o Two elements with the same priority are processed according to
the order in which they were added to the queue.
Example
 Time sharing systems: Programs of high priority are processed
first.
One-way list Representation
of
Priority queues
One-Way List Representation
 Each node in the list contains three types of information fields
(Information INFO, Priority Number PRN, and a link LINK).

 A node X precedes a node Y in the list when:


o X has higher priority than Y.
o Both X and Y have same priority but X was added before Y.
Deletion in a Priority Queue
1. Set ITEM = INFO [START].

2. Delete First node from the list.

3. Process ITEM.

4. Exit.
Insertion in a Priority Queue
1. Traverse the One-Way list until finding a node X whose priority
number exceeds N.

2. Insert ITEM in front of node X.

3. If no such node is found, insert the ITEM as the last element of the
list.

4. Exit.
1.# include<stdio.h> 1.// delete method
2.# include<malloc.h> 2.
3. 3.void del()
4.typedef struct node 4.{
5. { 5. NODE *temp;
6. int priority; 6. // condition to check whether the Queue is empty or not
7. int info; 7. if(front == NULL)
8. struct node *link; 8. printf("Queue Underflow\n");
9.}NODE; 9. else
10.NODE *front = NULL; 10. {
11. 11. temp = front;
12.// insert method 12. printf("Deleted item is %d\n", temp->info);
13.void insert(int data,int priority) 13. front = front->link;
14.{ 14. free(temp);
15. NODE *temp,*q; 15. }
16. 16.
17. temp = (NODE *)malloc(sizeof(NODE)); 17.// display method
18. temp->info = data; 18.void display()
19. temp->priority = priority; 19.{
20. // condition to check whether the first element is empty or 20. NODE *ptr;
the element to be inserted has more priority than the first elemen 21. ptr = front;
t 22. if(front == NULL)
21. if( front == NULL || priority < front->priority ) 23. printf("Queue is empty\n");
22. { 24. else
23. temp->link = front; 25. {
24. front = temp; 26. printf("Queue is :\n");
25. } 27. printf("Priority Item\n");
26. else 28. while(ptr != NULL)
27. { 29. {
28. q = front; 30. printf("%5d %5d\n",ptr->priority,ptr->info);
29. while( q->link != NULL && q->link- 31. ptr = ptr->link;
>priority <= priority ) 32. }
30. q=q->link; 33. }
31. temp->link = q->link; 34.}
32. q->link = temp; 35./*End of display*/
33. } 36.
34.} 37.// main method
1.// main method
2.int main()
3.{
4. int choice, data, priority;
5. do
6. {
7. printf("1.Insert\n");
8. printf("2.Delete\n");
9. printf("3.Display\n");
10. printf("4.Quit\n");
11. printf("Enter your choice : ");
12. scanf("%d", &choice);
13. switch(choice)
14. {
15. case 1:
16. printf("Enter the data which is to be added in the queue : ");
17. scanf("%d",&data);
18. printf("Enter its priority : ");
19. scanf("%d",&priority);
20. insert(data,priority);
21. break;
22. case 2:
23. del();
24. break;
25. case 3:
26. display();
27. break;
28. case 4:
29. break;
30. default :
31. printf("Wrong choice\n");
32. }
33. }while(choice!=4);
34.
35. return 0;
36.}
Array Representation
of
Priority Queues
Array Representation of Priority Queues
 Use a separate queue for each level of priority (for each priority
number).

 Each such queue will appear in its own circular array and have its
own pointers FRONT and REAR.

FRONT REAR
1 2 3 4 5
2 4 [ X Y Z ]
3 3 [ P ]
0 0 [ ]
[ A B D ]
5 2
Deletion in a Priority Queue
 Delete and process the first element in a priority queue
maintained by a two-dimensional array QUEUE.

1. [Find the first non-empty queue.]


Find the smallest K such that FRONT [K] ≠ NULL.

2. Delete and process the front element in row K of QUEUE.

3. Exit.
Insertion in a Priority Queue
 Insert an ITEM with priority number M to a priority queue
maintained by a two-dimensional array QUEUE.

1. Insert ITEM as the REAR element in row K of QUEUE.

2. Exit.

You might also like