Program to implement stacks using arrays

// Program to implement stacks using arrays

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

#define max 20

class stack
{
	private:
		int st[max];
		int top;

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

void stack :: push()
{
		int item;

		if(top==max-1)
				cout<<"\nStack full";
		else
		{
			cout<<"\nEnter item to insert: ";
			cin>>item;
			st[++top]=item;
			cout<<"\nItem inserted";
		}
}

void stack :: pop()
{
		if(top==-1)
			cout<<"\nStack empty";
		else
		{
			cout<<"\nDeleted element is: "<<st[top];
			top--;
		}
}


void stack :: display()
{
		 if(top==-1)
			cout<<"\nStack empty";
		 else
		 {
			cout<<"\nElements of stack are: ";
			for( int i=top;i>=0;i--)
				cout<<st[i]<<"  ";
		 }
}

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