PROGRAM TO IMPLEMENT QUEUE USING CIRCULAR LIST.
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
class Queue;
class Node
{
private:
int Info;
Node *Link;
friend class Queue;
};
class Queue
{
private:
Node *Last;
public:
Queue()
{
Last = NULL;
}
void EnQueue(int Value)
{
Node *Temp = new Node;
Temp->Info = Value;
if (IsEmpty())
Last = Temp;
else
Temp->Link = Last->Link;
Last->Link = Temp;
Last = Temp;
}
int dQueue()
{
int Value;
Node *Temp = new Node;
if (IsEmpty())
{
cout << "\nLIST IS EMPTY.....!!!\n" << endl;
exit(1);
}
Temp = Last->Link;
Value = Temp->Info;
if(Temp == Last)
Last = NULL;
else
Last->Link = Temp->Link;
delete Temp;
return Value;
}
inline bool IsEmpty()
{
return Last == NULL;
}
};
int main()
{
Queue Obj;
Obj.EnQueue(1);
Obj.EnQueue(2);
Obj.EnQueue(3);
Obj.EnQueue(4);
Obj.EnQueue(5);
cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;
cout << Obj.dQueue() << endl;
getch();
return 0;
}
0 comments:
Post a Comment