Saturday, June 16, 2012

Data Structure and Algorithm of QUEUE

A queue is an ordered collection of items from which items may be deleted at one end(called front and head of the queue) and into which items may be inserted at the other end (called the rear end on tail of the queue). It is First-In-First-Out(FIFO).There are two types of queue-Linear Queue and Circular Queue.





En-queue is the process of appending data to queue.
De-queue is the process of removing data from queue.

Algorithms for Linear Queue
1)Both the head and tail pointer vary




a)Declare and initialize necessary  variables
               front=0,rear=-1,Max_size
b)For En-queue operation
           IF rear>=Max_Size-1
               Print”Queue is Full !”
          ELSE
                rear=rear+1
                queue[rear]=item
c)For next  En-queue operation go to step2
d)For De-queue operation
          IF front>rear
              Print “Queue is Empty!”
          ELSE
              Item=queue[front]
             front=front+1
e)For De-queue of next data items go to step 4
f)Stop


2)Head pointer fixed tail pointer vary


a)Declare and initialize necessary variables
 rear=-1
b)For en-queue operation
           IF rear>=Max_Size-1
               Print “Queue  is  Full !”
            ELSE
                rear=rear+1
               queue[rear]=item
c)For En-queue  of next data item go to  step 2
d)For De-queue operation
               IF rear=-1
                    Print “  queue  is Empty”
               ELSE
                    Item=queue[rear]
          swap the elements from upper to one step lower
                rear=rear-1
e)For De-queue for next data item go to step 4
f)Stop


CIRCULAR  QUEUE

a)Declare and initialize necessary variables such  as head=0,tail=0
b)For En-queue operation
            IF   head==(tail+1)%Max_Size
                  Print” Queue is Full !”
            ELSE
                  queue[tail+1]=item
                  Tail=(tail+1)%Max_Size
c)For En-queue of next data item go to step 2
d)For De-queue operation
            IF head =tail
                 Print “Queue is Empty!”
           ELSE
               remove  item i.e. item =queue[head]
             set head=(head+1)%Max_Size
e)For De-queue of next data item goto step 4
f)Stop

No comments:

Post a Comment