Let’s get to the point, what’s the different beetwen Circular and Linear? Just like previous post, the different is the head is always move around or you can say dynamic. The first and the last data is likely become linked to another one. If we do enqueue, the tail will be point to the new data. If you can see the picture below, you will know that the data are never move like linear queue, but the head node. This head node always move when you do dequeue and enqueue new data.
While we using circular queue, we can use mod (%) to help us for the head movement. Here is the example of Circular Queue. This code is the class of Queue, it’s just made for keep integer data up to small size, it’s just for simple example program of Circular Queue.
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#define MAX 5
class Cqueue
{
private:
int element[MAX];
int head;
int tail;
public:
Cqueue()
{
head=0;
tail=0;
}
void enqueue(int x)
{
if(!full())
{
element[(head+tail)%MAX]=x;
tail++;
}
}
int dequeue()
{
if(!empty())
{
int temp;
temp=element[head];
head++;
head=head%MAX;
tail--;
return temp;
}
}
int full()
{
if(tail==MAX)
return 1;
else
return 0;
}
int empty()
{
if(tail==0)
return 1;
else
return 0;
}
void view()
{
for(int i=0;i<tail;i++)
cout<<element[(i+head)%MAX]<<endl;
getch();
}
};
Popularity: 43% [?]







