Skip to main content

Definition of Queue and its types with algorithms (Data structure)

                               Queue 

A queue is non-premitive linear data structure . It is based on first-in-first-out (FIFO) mechanism.In queue deletion can take place at one end of the list , the front of the list. And insertion can take place at the other end of the list , rear of the list.
    Queues are often usde in programming networks , operating systems , and other situations in which many different processes must share resources such as CPU time.



Image result for queue in ds



* Working Process on Queue :-
  • By incrementing rear pointer we can insert an element in queue.
  • By incrementing front pointer we can removew element from queue.
  • If queue is empty then front and rear pointer must be incremented by 1 first time.
  • When rear=size- 1 then queue is full.
  • When front=rear then reset front=-1 and rear=-1.
  • When front=rear=-1 then the queue is empty.


Queue is different from stack in many cases. Both are  non-premitive linear data structure but the mechanisms on insertion and deletion are fully different. Where queue is based on FIFO mechanism, stack is based on LIFO mechanism.

Best example of queue is the row at the ticket counter for any shows. The person who came first take ticket first , thus follows FIFO rule. 

Image result for queue



Many operations can be possible on a queue as:-

  • push (insert )
  • pop (delete)
  • peep etc.

Related image

* Algorithm for insertion (push operation):-
   
   Step 1: [ Check for ovetflow condition]
                 If  rear>= size of array  then
                 Output:-"queue is overflow" and exit.

   Step 2: [Increment rear pointer]
                rear=rear+1

   Step 3: [Insert an element]
              Q[rear]=value

   Step  4: [Set the front pointer]
               If
                front =0  then,
                front =1

    step 5:    return


* Algorithm for deletion (pop operation):- 

Step 1: [check for underflow condition]
             If front =0  then,
             output:- "queue is underflow" and exit.

Step 2: [remove the element]
             value=Q[rear]

Step 3: [check for empty queue]
           If  front =rear  then,
              front=0
              rear=0
          Else  
               front =front+1

Step 4:   return

Related image
* There are mainly three types of queues are possible:--

i> Circular queue.
ii> Double ended queue.
iii> Priority queue.

i> Circular Queue:- 
    In a normal queue , we can insert elements untill queue becomes full. But once queue becomes full      , we cannot insert the next element . The circular is dsigned to overcome the limitations of simple        queue.
       Circular Queue is a linear data structure in which the operations are performad based on FIFO      principle and last position id connected back to the first position to make a circle.

  Suppose we have an array Q that contains n elements in which Q[1] comes after Q[n] pocket in the    array. When this technique is used then the queue is called circular queue. Thus we can say that a       queue is called circular when the last room comes just before the first  room.

     

enter image description here

* Algorithm to insert element in circular queue:-

 Step 1: [Check for overflow condition ]
              If 
                front=1 and rear=n  then,
              output:-  "queue is overflow" and exit.

 Step 2: [Insert element in the queue]
            Else if 
                   front=0  then,
                    front=1
                     rear=1
                    Q[rear]=value 

Step 3: [check if the rear at the end of the queue]
              Else if
                     rear=n  then,
                      rear=1
                    Q[rear]=value

Step 4: [Insert the element]
             Else 
                  rear=rear+1
                 Q[rear]=value

Step 5:        return


* Algorithm to delete an elment from a circular queue:-

Step 1: [Check for underflow condition]
            If   
             front =0  then,
            output:- "Queue is underflow" and return.

Step 2: [remove the element]
            value=Q[front]

Step 3: [Check whether the queue is empty or not]
             If  
               front =rear   then
               front =0
               rear=0

Step 4: [Check the front pointer position]
            Else if 
                    front =n   then,
                    front =1
          Else
                  front =front +1

Step 5: [Return the removed element]
            return(value)


circular 

  Rest two types of queue are remaining .
 Will be publish on next post.😊

Comments

Popular posts from this blog

Process Scheduling And Types of Process Schedular :-

        ⇰ PROCESS SCHEDULING Process Scheduling  is a task  of Operating System that schedules processes of different states like new, ready, waiting, terminated  and running.This scheduling helps in allocation of CPU time for each process, and Operating System allocates the CPU time for each procss. And the process scheduling plays important role to keep the CPU busy all the time.  ⏩   Followings are some objectives of Process Scheduling :-  i > To increase the amount of users within acceptable response times.  ii > To maintain the balance between response and utilization of system. iii > To decrease the enforce priorities and  give reference to the processes holding the key resources.      ⇰  PROCESS SCHEDULAR A scheduler carries out the pro cess scheduling work. Schedulers are often implemented so they keep all computer resources busy and  allows multiple users to shar...

Process & Its state And process control block :-

                ⇰  PROCESS :- A process can be thought of as a program in execution. Means when any program is executed it becomes process. A processwill need certain resources such as CPU time , memory, files and I/O devices to complete its task. These resources are allocated to the process either when it is created or at the time of execution.             A process is the unit of work in most systems. A system consistes of a collection of processes. All these processes may execute concurrently. Traditionally a process contained only a single thread. Most modern operating ststems now supports processes that have multiple threads.         The operating system is responsible for several important works of process management as - the creation and deletion of process, the schrduling of process, communication and deadlock handling of process. Process is broudly divided into two ...

Tokens and its types in 'C'

   Tokens are the smallest individual unit of a program or in simple words it is a main part of C program.Tokens are the building blocks of any program. The smallest individual and basic unit of a C programming is called c tokens.      *    Normally there are six types of tokens in C:- i> Keywords:-          Keywords are special words that are used to give a special meaning to the program and can't be used as variable and constant.They are basically a sequence of characters that have fixed to mean. For example:-                 auto     double      long     break                 float    short        char     if                while    continue   int       void etc. All...