Lecture Notes Data Structures and Algorithms
 
Barani Institute of Information Technology, Rawalpindi 1/4
 
Week 12
 
Contents
• Circular Queue
• Primitive Operations of Circular Queue
• Array Implementation of Circular Queue
• Link List Implementation of Circular Queue
 
Circular Queue
This is another useful representation of a queue is circular which overcomes the limitations of
simple queues. In this way we assume that our queue is circular in nature, means final address is
followed by first address.
Here we arrange the element queue[1], queue[2],…..,queue[n] in a circular fashion, with
queue[1] follows queue[n]. Following fig. illustrates such a representation.
Primitive Operations of Circular Queue
ALGORITHM (Circularqueuedelete)
If front = -1
Then queue underflow
Else A = queue[front]
If front = rear
Begin
Front = -1
Front 
Rear 
Lecture Notes Data Structures and Algorithms
 
Barani Institute of Information Technology, Rawalpindi 2/4
 
Rear = -1
End
Else
If front = maxqueuesize
Then front = 0
Else front = front + 1
ALGORITHM (Circularqueueinsert)
If ((front=1) && (rear = maxqueuesize-1) || (front=rear+1))
Then queue overflow
If (front=-1) [initially empty]
Then rear = 0; front=0
Else if rear = maxqueuesize -1
Then rear =0
Else
Rear=rear+1
Queue[rear]=item
Array Implementation of Circular Queue
class Cqueue 
{ 
  int q[10]; 
  int front; 
  int Rear; 
              public: 
  Cqueue()   { 
    front=‐1; 
    Rear=‐1; 
  } 
  void insert(int d)  { 
                     if((front==0 && Rear==9) || front==Rear+1) 
    cout<<"Overflow"; 
        if(front==‐1)  { 
    front=0; 
    Rear=0; 
        } 
       else if(Rear==9)  { 
    Rear=0; 
       } else { 
    Rear=Rear+1; 
                    } 
       q[Rear]=d; 
             } 
  int remove()   { 
                   if(Rear==‐1)   { 
Lecture Notes Data Structures and Algorithms
 
Barani Institute of Information Technology, Rawalpindi 3/4
 
    cout<<"Overflow"; 
                            return; 
                   } 
                   int n=q[front]; 
                   q[front]=NULL; 
                   if(front==Rear)  { 
                    front=‐1; 
                    Rear=‐1; 
                   }  else if(front==9)   { 
                    front=0; 
                   }  else 
                    front=front+1; 
                   return n; 
      } 
}; 
   
Link List Implementation of Circular Queue
struct link 
{ 
  int data; 
  link *next; 
}; 
class CQueue 
{ 
  link *front; 
public: 
  CQueue()   { 
    front=NULL; 
  } 
  void Insert(int d)   { 
                    link *newlink=new link; 
       newlink‐>data=d; 
       if(front==NULL)  { 
    front=newlink; 
    newlink‐>next=front; 
       }  else { 
    link *current=front; 
    while(current‐>next!=front) 
    { 
      current=current‐>next; 
    } 
    current‐>next=newlink; 
    newlink‐>next=front; 
  } 
} 
Lecture Notes Data Structures and Algorithms
 
Barani Institute of Information Technology, Rawalpindi 4/4
 
 
  int remove()   { 
                    if(front==NULL)  { 
    cout<<"EMPTY"; 
       }  else  { 
    link *temp=front; 
    front=front‐>next; 
    link *current=front; 
    while(current‐>next!=temp) 
    { 
      current=current‐>next; 
    } 
    int n=temp‐>data; 
    current‐>next=front; 
    delete temp; 
    return n; 
  } 
         } 
}; 

Dsa circular queue

  • 1.
    Lecture Notes DataStructures and Algorithms   Barani Institute of Information Technology, Rawalpindi 1/4   Week 12   Contents • Circular Queue • Primitive Operations of Circular Queue • Array Implementation of Circular Queue • Link List Implementation of Circular Queue   Circular Queue This is another useful representation of a queue is circular which overcomes the limitations of simple queues. In this way we assume that our queue is circular in nature, means final address is followed by first address. Here we arrange the element queue[1], queue[2],…..,queue[n] in a circular fashion, with queue[1] follows queue[n]. Following fig. illustrates such a representation. Primitive Operations of Circular Queue ALGORITHM (Circularqueuedelete) If front = -1 Then queue underflow Else A = queue[front] If front = rear Begin Front = -1 Front  Rear 
  • 2.
    Lecture Notes DataStructures and Algorithms   Barani Institute of Information Technology, Rawalpindi 2/4   Rear = -1 End Else If front = maxqueuesize Then front = 0 Else front = front + 1 ALGORITHM (Circularqueueinsert) If ((front=1) && (rear = maxqueuesize-1) || (front=rear+1)) Then queue overflow If (front=-1) [initially empty] Then rear = 0; front=0 Else if rear = maxqueuesize -1 Then rear =0 Else Rear=rear+1 Queue[rear]=item Array Implementation of Circular Queue class Cqueue  {    int q[10];    int front;    int Rear;                public:    Cqueue()   {      front=‐1;      Rear=‐1;    }    void insert(int d)  {                       if((front==0 && Rear==9) || front==Rear+1)      cout<<"Overflow";          if(front==‐1)  {      front=0;      Rear=0;          }         else if(Rear==9)  {      Rear=0;         } else {      Rear=Rear+1;                      }         q[Rear]=d;               }    int remove()   {                     if(Rear==‐1)   { 
  • 3.
    Lecture Notes DataStructures and Algorithms   Barani Institute of Information Technology, Rawalpindi 3/4       cout<<"Overflow";                              return;                     }                     int n=q[front];                     q[front]=NULL;                     if(front==Rear)  {                      front=‐1;                      Rear=‐1;                     }  else if(front==9)   {                      front=0;                     }  else                      front=front+1;                     return n;        }  };      Link List Implementation of Circular Queue struct link  {    int data;    link *next;  };  class CQueue  {    link *front;  public:    CQueue()   {      front=NULL;    }    void Insert(int d)   {                      link *newlink=new link;         newlink‐>data=d;         if(front==NULL)  {      front=newlink;      newlink‐>next=front;         }  else {      link *current=front;      while(current‐>next!=front)      {        current=current‐>next;      }      current‐>next=newlink;      newlink‐>next=front;    }  } 
  • 4.
    Lecture Notes DataStructures and Algorithms   Barani Institute of Information Technology, Rawalpindi 4/4       int remove()   {                      if(front==NULL)  {      cout<<"EMPTY";         }  else  {      link *temp=front;      front=front‐>next;      link *current=front;      while(current‐>next!=temp)      {        current=current‐>next;      }      int n=temp‐>data;      current‐>next=front;      delete temp;      return n;    }           }  };