If Any Required Program Please Ask In Comment I Will Help You(any Program in JAVA or C++) . . THANKS FOR VISITING MY BLOG!

If U LIKE MY PROFILE RAISE YOUR HAND IF U NOT RAISE UR STANDARD. Powered by Blogger.

Friday, November 30, 2018

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