Professional Documents
Culture Documents
LABORATORY
Queue
# define maxsize 5
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;
}
}
};
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:
Exercises: