Queues in Data Structure Using C
Queues in Data Structure Using C
Objectives: Describe a queue Representation of queue in computer memory Implementation of various operation on queues Describe priority queue Representation of priority queue in computer memory Application of queues
Introduction
A queue is a line of persons waiting for their turn at some service counter. Service counter can be a ticketing window of a cinema hall or ticketing window of railway station etc. Depending on the type of service provided by the service counter and number of person interested in service, there could be queue of varying lengths. The service at the service counter is provide on the first come first serve (FCFS) basis, i.e., in order of their arrival in the queue.
Bus Stop
Suppose that at service counter, t1 units time is needed to provide a service to a single person and on average a new person arrive at the counter in t2 units of time. The following possibilities may arise: 1. If t1<t2, then the service counter will be free for some time before a new person arrives at the service counter. Hence no queue in this case. 2. If t1>t2, then service counter will be busy for some time even after the arrival of a new person. Therefore this person has to wait for some time. Similarly other persons arriving at the service counter have to wait. Hence a queue will be formed. 3. If t1=t2, then as a person leaves the service counter, a new person arrives at the service counter. Hence no queue in this case also.
Definition
A queue is a linear list in which insertion can take place at one end of the list, called the rear of the list, and deletion can take place at other end called front of the list. Behavior of the queue is FIFO system. In queue terminology insertion and deletion operations are known as enqueue and dequeue operations.
An empty queue 0 1 2 3 4 5 6 7 8 9
5
front
12
12
front
9
rear
Operations on queues
The following operations are performed on queues: Create queue(q)to create queue as an empty queue. Enque(q,i)to insert element I in a q. Dequeue(q)to access and remove an element of queue q. Peek(q)to access the first element of the queue q without removing it. Isfull(q)to check whether the queue q is full. Isempty(q)to check whether the queue q is empty.
Array representation
The following are the necessary declaration: #define CAPACITY 10 typedef struct { int front; int rear; int elements[CAPACITY]; }queue; queue q; In addition to above decoration we will use the declaration typedef enum {false, true } Boolean;
5
0
7
1
12 8
2 3
9
4 5 6 7 8 9
2 front
4 rear
12 8
12 8
10 11 20
It is not possible to enqueue more elements as such, though two position in the linear queue are vacant.
To overcome this problem, the elements of the queue are moved forward, so that the vacant positions are shifted toward the rear end. 0 1 2 3 4 5 6 7 8 9
0 front 7 rear
12 8
10 11 20
Circular queue
This difficulty of managing front and rear can be overcome if we treat the queue position with index 0 as a position that comes after position with index 9 i.e., we treat the queue as circular.
4 5 2 3
-1 front
-1 rear
1
7 8 9
Representation of a queue
4 5
8
2 12
0 front
4 rear
6 7 1 5 7 0
Representation of a queue
4 5
8
2 12
2 front
4 rear
6 1 7
Representation of a queue
4 5 9 6 3
8
2 12
2 front
9 rear
1 10 7 11 20 0
Representation of a queue
4 5 9 6 3
8
2 12
2 front
0 rear
1 10 7 11 60 20 0
12
12
12
10
11
Enqueue operation
void enqueue(queue *pq, int value) { node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->info=value; ptr->next=NULL; if(pq->rear==NULL) /*queue initially empty*/ pq->front=pq->rear=ptr; else (pq->rear)->next=ptr; pq->rear=ptr;
Dequeue operation
Int dequeue(queue *pq) { int temp; node *ptr; temp=(pq->front)->info; ptr=pq->front; if(pq->front==pq->rear) /*only one element*/ pq->front=pq->rear=NULL; else pq->front=(pq->front)->next; free(ptr); return temp;
Disposing a queue
Void disposequeue(queue *pq) { node *ptr; while(pq->front!=NULL) { ptr=pq->front; pq->front=(pq->front)->next; free(ptr); } pq->rear=(node*)NULL; }
Types of Dqueue
Types of dqueue are due to the restrictions put to perform either insertion or deletion only at one end
Input restricted queue Output restricted queue
Deque
F Deletion Insertion 10
DQ(0)
R 20
DQ(1)
30
DQ(2)
40
DQ(3)
50
DQ(4)
Insertion Deletion
Operations on Deque
Insertion of an element at the rear end of the queue Deletion of an element from the front end of the queue Insertion of an element at the front end of the queue Deletion of an element from the rear end of the queue
Multiple Queues
Problems with the queues represented with arrays are: We have to allocate large space to avoid the overflow. Large space will be wasted due to less size of queue. There exits a trade-off between the number of overflows and the space. One possibility to reduce this tradeoff is to represent more than one queue in the same array of sufficient size
f[1] Queue1 r[1] f[2] Queue2 r[2]f[3] r[3]f[4] r[4]f[5] r[5]
Queue3
Queue4
Queue5
Priority Queues
A priority queue is a kind of queue in which each element is assigned a priority and the order in which elements are deleted and processed comes fro the following rules: An element of highest priority is processed first before any element of lower priority. Two or more elements with the same priority are processed according to the order in which they are added to the queue.
Applications of Queues
There are several algorithms that use queues to solve problems efficiently. Eg. We have to use queue for level order traversal in binary tree. Eg. Performing BFS on graph. Eg. Most of the simulation related problems. When jobs are submitted to a networked printer, they are arranged in order of arrival. Thus , essentially jobs sent to a printer are placed on a queue. There is a kind of computer network where disk is attached to one computer, known as file server. User on other computers are given access to files on a first come first serve basis, so the data structure is queue. Vertually every real life line a queue. For example, lines at ticket counters at cenema halls, railway stations, bus stands, etc., are queues because the service, i.e. ticket, is provided on first come first serve basis.