Thursday, January 14, 2010

Circular Queue

A circular queue is a Queue but a particular implementation of a queue. It is very efficient. It is also quite useful in low level code, because insertion and deletion are totally independent, which means that you don't have to worry about an interrupt handler trying to do an insertion at the same time as your main code is doing a deletion.

In queue, we take out the value at front and put the value at rear.

Algorithm for Insertion:-
Step-1: If "rear" of the queue is pointing to the last position then go to step-2 or else step-3
Step-2: make the "rear" value as 0
Step-3: increment the "rear" value by one
Step-4:
Step-4.1: if the "front" points where "rear" is pointing and the queue holds a not NULL value for it, then its a "queue overflow" state, so quit; else go to step-4.2
Step-4.2:insert the new value for the queue position pointed by the "rear"

Algorithm for deletion:- 
Step-1: If the queue is empty then say "empty queue" and quit; else continue
Step-2: Delete the "front" element
Step-3: If the "front" is pointing to the last position of the queue then step-4 else step-5
Step-4: Make the "front" point to the first position in the queue and quit
Step-5: Increment the "front" position by one

Circular Queue implementation in c/cpp
#include
using namespace std;
#include

int c=0;

struct node
{
       int info;
       struct node *next;
};
struct node *top;

int empty()
{
    return((top == NULL)? 1:0);
}

void insert(int n)
{
     struct node *p;
     p=new node;
     if(p!=NULL)
     {
              
                c++;
                p->info=n;
                if(empty())
                           top=p;
                else
                    p->next=top->next;
                top->next=p;
                top=p;                
     }
     else
         cout<<"Not inserted,No memory available";
}

int del()
{
  
    c--;
    struct node *temp;
    int x;
    temp=top->next;
    x=temp->info;
    if(temp==top)
                top=NULL;
    else
        top->next=temp->next;
    free(temp);
    return(x);
}

void print()
{
     int i =0;
     struct node *temp;
     cout<<"\n\t\t";

if(c==0)
                   cout<<"\n\t\tNo elements\n";
     else
     {
         temp = top->next;
         while(i!=c)
    {
                      cout<info<<"  ";
                      temp=temp->next;
                      i++;
         }
    cout<
      }
}

int main()
{
    int s,n;
    cout<<"\n\t\tCIRCULAR QUEUE IMPLEMENTATION\n";
    while(1)
    {
            cout<<"\n\t\t1>Push\n";
            cout<<"\t\t2>Pop\n";
            cout<<"\t\t3>Print\n";
            cout<<"\t\t4>Exit\n";
            cout<<"\t\tEnter your choice:";
            cin>>s;
            switch(s)
            {
                  case 1:
                          cout<<"\n\t\tEnter the number:";
                          cin>>n;
                          insert(n);
                          break;
                
                  case 2:
                
                          if(empty())
                          {
                             cout<<"\n\t\tStack underflow\n";
              
                          }
                          else
                          {
                             n=del();
                             cout<<"\n\t\tThe pop value is:"<<
                          }
                          break;
                      
                  case 3:
                          print();
                          break;
                  case 4:
                          exit(1);
    }
    }
}
  

0 comments:

Post a Comment