More Related Content
Similar to Linked Stack program.docx (20)
Linked Stack program.docx
- 1. /* linked stack*/
#include <iostream>
using namespace std;
class Node
{
public :
int data;
Node *link;
};
class Lstack
{
private:
Node *Top;
public:
Lstack()
{
Top= NULL;
}
void Push(int value);
int Pop();
void Display();
};
void Lstack::Push(int value)
{
Node *Newnode;
Newnode = new Node;
Newnode->data=value;
Newnode->link = NULL;
if(Top==NULL)
{
Top=Newnode;
}
else
{
Newnode->link=Top;
Top=Newnode;
}
}
int Lstack::Pop()
{
Node* Temp=Top;
int popval;
if(Temp == NULL)
cout << "Empty List";
else
{
popval=Temp->data;
Top=Temp->link;
delete Temp;
}
- 2. return popval;
}
void Lstack::Display()
{
Node *temp = Top;
if(temp == NULL)
cout << "Empty List";
else
{
while(temp != NULL)
{
cout << temp->data << "t";
temp = temp->link;
}
}
cout << endl;
}
int main()
{
Lstack L1;
L1.Push(35);
L1.Push(45);
L1.Push(55);
cout<<"n"<<"Before Popping the values in stack : ";
L1.Display();
cout<<"Popped value is : "<<L1.Pop();
cout<<"n"<<"After Popping the values in stack: ";
L1.Display();
return 0;
}
Output:
Before Popping the values in stack : 55 45 35
Popped value is : 55
After Popping the values in stack: 45 35