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