Professional Documents
Culture Documents
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’.
5. Exit
Deletion in a Queue
LINK_Q_DELETE (INFO, LINK, FRONT, REAR, AVAIL, ITEM)
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.
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);
if (*pfront == -1) {
printf("\nDeque is empty.\n");
return 0;
}
item = arr[*prear];
arr[*prear] = 0;
(*prear)--;
if (*prear == -1)
*pfront = -1;
return item;
}
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.
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.
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.
2. Exit.