Thursday, 23 November 2017

Queue in data structure



Queue

A queue is a linear list of elements in which deletions can take place only at one end, called the " front " and insertion can take place only at the other end, called " rear ". The term " front " and " rear " are used in describing a linear list only when it is implemented as a queue.


Queues are also called " first-in first-out " (FIFO) list. Since the first element in a queue will be the first element out of the queue. In other words, the order in which elements enter in a queue is the order in which they leave.

Practical daily life : the people waiting in a line at Railway ticket Counter form a queue, where the first person in a line is the first person to get ticket.

In computer world : In timesharing system, in which programs with the same priority form a queue while waiting to be executed.


Operation on Queue

enqueue : To insert an element into the queue

dequeue : To delete an element from the queue

display : To display the elements of the queue

Representation of Queue using array

The implementation of queue data structure using array is very simple, just define a one dimensional array of specific size and insert or delete the values into that array by using FIFO (First In First Out) principle with the help of variables 'front' and 'rear'.

Algorithm to insert an element in a queue

Step 1: IF REAR == MAX-1 .

Write " Queue is full !!! Insertion is not possible !!! "

Go to step EXIT

[ End of IF ]

Step 2: IF FRONT == -1 and REAR ==-1

Set FRONT = REAR = 0

ELSE

REAR = REAR + 1

[ End of IF ]

Step 3: Set Queue[ REAR ] = NUMBER

Step 4: EXIT

Initially REAR = -1 and FRONT = -1 which represent that queue is empthy , when the first number is inserted in the queue REAR and FRONT both becomes zero. From the next onward , for any insertion FRONT will not be changed only REAR will be incremented by one for each insertion.



Algorithm to delete an element from a queue


Step 1: IF FRONT == -1 OR FRONT == REAR + 1.

Write " Queue is empty !!! Deletion is not possible !!! "

ELSE

NUMBER = Queue[ FRONT ]

FRONT = FRONT + 1

[ End of IF ]

Step 3: IF FRONT > REAR OR FRONT == REAR + 1.

Set FRONT = REAR = -1

[ End of IF ]

Step 4: EXIT





Share this

0 Comment to "Queue in data structure"

Post a Comment