Program to implement stacks using lists


// Program to implement stacks using lists

#include<iostream.h>
#include<conio.h>
#include<process.h>

#define null 0

class stack
{
	private:
		struct node
		{
			int data;
			node *next;
		};
		node *top;

	public:
		stack()
		{
			top=null;
		}
		void push();
		void pop();
		void display();
};

void stack :: push()
{
	node *x=new node;

	cout<<"\nEnter item to insert: ";
	cin>>x->data;

	x->next=top;
	top=x;
	cout<<"\nItem inserted";
}


void stack :: pop()
{
	node *x;

	if(top==null)
		cout<<"\nStack empty";
	else
	{
		x=top;
		cout<<"\nDeleted element is: "<<top->data;
		top=top->next;
		delete(x);
	}
}


void stack :: display()
{
	node *x;
	
	if(top==null)
		cout<<"\nStack empty";
	else
	{
		cout<<"\ nElements of stack are: ";
		for(x=top;x!=null;x=x->next)
			cout<<x->data<<"->";
		cout<<"null";
       }
}


void main()
{
	stack is;
	int c;
	clrscr();

	while(1)
	{
		cout<<"\n\n1.Push\n2.pop\n3.display\n4.exit\n";
		cout<<"\nEnter your choice: ";
		cin>>c;

		switch(c)
		{
			case 1:
					is.push();
					break;
			case 2:
					is.pop();
					break;
			case 3:
					is.display();
					break;
			case 4:
					exit(0);
		}
	}
}

Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in