Another data structures type is queue. Adding data is the same as stack, it’s called enqueue. The different between stack is queue use “first in first out”. The first data will be kicked out first when you dequeue it (getting data from queue). There are 2 pointers we can use in queue. Head is pointer that point the first data and tail to point the last data. This picture below may help you to understand.

Linear Queue
There are 2 types of queue, linear and circular. A circular queue has a shape like donut, so we can say the queue is connected. The different is at head position, a linear queue has a static head position, and a circular has a dynamic head position every time you do dequeue.

Circular Queue
This time, I will only tell you how to make a linear queue class in header :D, it looks like stack application at previous post, so it’s unnecessary to write the main code.
//============== start of code ==============//
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define MAX 255
class lqueue
{
private:
int data[MAX];
int tail; // linear queue has a static head, assume head = 0
public:
lqueue()
{
tail = 0;
}
void enqueue(int value)
{
if (!full())
{
data[tail] = value;
tail++;
}
}
int dequeue()
{
if (!empty())
{
int temp;
temp = data[head];
for (int i = 0; i < tail; i++)
{
data[i] = data[i+1];
}
tail--;
return temp;
}
}
bool full()
{
if (tail == MAX)
return 1;
else
return 0;
}
bool empty()
{
if (tail == 0)
return 1;
else
return 0;
}
void view()
{
for (int i = 0; i < tail; i++)
printf("%d ",data[i]);
getch();
}
};
//=============== end of code ===============//
It’s done. I think it should work fine :))
Popularity: 37% [?]





