You are on page 1of 7

Data Structure

LABORATORY

Lab. Sheet five

Queue

Computer engineering department


Second class
Linear queue:
#include <iostream>
using namespace std;

# define maxsize 5

template <class T>


class ArrayQueue
{
private:
T qu[maxsize];
int front,rear;
int count;
public:
ArrayQueue()
{
front=rear=-1;
count=0;
for(int i=0;i<maxsize;i++)
qu[i]=NULL;
}
void insert(T item)
{
if(count==maxsize)
{
cout<<"Queue is Full"<<endl;
return;
}
if(rear==maxsize-1||rear==-1)
{
qu[0]=item;
rear=0;
if (front==-1) front=0;
}
else
qu[++rear]=item;
count++;
}
T remove()
{
if(isEmpty())
{
cout<<"Queue is Empty"<<endl;
return NULL;
}
T tmp=qu[front];
qu[front]=NULL;
if (front==rear) rear=front=-1;
else if (front ==maxsize-1) front=0;
else front++;
count--;
return tmp;
}
T peek()
{
return qu[front];
}
bool isEmpty()
{
return (count==0);
}
int size()
{
return count;
}
void DisplayAll()
{
cout<<"Queue:";
for(int i=0;i<maxsize;i++)
{
if(qu[i]==NULL)cout <<'#'<<" ";
else cout<<qu[i]<<" ";
}
}
};

int _tmain(int argc, _TCHAR* argv[])


{
ArrayQueue <char> q;
q.insert('A');
q.insert('B');
q.insert('C');
q.insert('D');
q.insert('E');
q.insert('F');
q.insert('G');
q.DisplayAll();
q.remove();
q.remove();
cout<<endl;
q.DisplayAll();
return 0;
}
Link queue:
#include <iostream>

using namespace std;

template <class T>

class LinkQueue
{
private:
struct Node
{
T data;
Node * next;
};
Node * front,*rear;
int count;
public:
LinkQueue()
{
front=rear=NULL;
count=0;
}
void insert(T item)
{
Node *p=new Node;
p->data=item;
if(isEmpty())
{
front=rear=p;
rear->next=NULL;
}
if(front==rear)
{
rear=p;
front->next=rear;
rear->next=NULL;
}
else
{
rear->next=p;
rear=p;
rear->next=NULL;
}
count++;

}
T remove()
{
if(isEmpty())
{
cout<<"Queue is Empty"<<endl;
return NULL;
}
T item =front->data;
front=front->next;
count--;
return item;
}
bool isEmpty()
{
return (count==0);
}
T peek()
{
return front->data;
}
int size()
{
return count;
}
void DisplayAll()
{
Node*p=front;
while(p->next!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}

}
};

int _tmain(int argc, _TCHAR* argv[])


{

LinkQueue <char> q;
q.insert('A');
q.insert('B');
q.insert('C');
q.insert('D');
q.insert('E');
q.insert('F');
q.insert('G');
q.DisplayAll();
cout<<endl;
q.remove();
q.remove();
cout<<endl;
q.DisplayAll();
return 0;
}
Lab. Exercise:

Checking the performance of cashier in supermarket. If the cashier


spent (5 minutes) in average within each customer. At the end of working,
shift it serves (36 customer) in 3 hours.
In this situation, the casher starts serving queue that already have (5
customers).
Insert to each customer (Customer ID and serving time) and find after (3
hours) does the cashier reach its goal (36 customer) or not.

Exercises:

1. For this assignment, you will be implementing a class of your own: a


priority queue, which is a variation on the standard queue. The standard
queue processes element in the first-in, first-out ("FIFO") manner typical of
ordinary waiting lines. Queues can be handy, but a FIFO strategy is not
always, what is needed.
A hospital emergency room, for example, needs to schedule patients
according to priority. A patient with a more critical problem will pre-empt
others even if they have been waiting longer. This is a priority queue, where
elements prioritized relative to each other and when asked to dequeue one,
the highest priority element in the queue removed.
2. In circular queue, is queue where the last node connected back to the first
node to make a circle, both the front and the rear pointers points to the
beginning of the array.
In addition, priority queue is a data structure in which data added to and
removed from a queue, based on priority.
Write C++ to modify the basic queue class to implement a circular queue
that its removal function based on priority concept.

You might also like