Untitled

 avatar
user_7016102
plain_text
a month ago
1.4 kB
2
Indexable
Never
// Lab No 2

#include<iostream>
#include<stdlib.h>
using namespace std;

int stack[100], top = -1, n;

void push(int val)
{
	if(top>=n-1)
	{
		cout<<"Stack overflow"<<endl;
	}
	else{
		top++;
		stack[top] = val;
	}
}

void pop()
{
	if(top<=-1)
	{
		cout<<"Stack underflow"<<endl;
	}
	else{
		cout<<"The popped element is :"<<stack[top]<<endl;
		top--;
	}
}
void display()
{
	if(top>=0)
	{
		cout<<"The stack elements are :"<<endl;
		for(int i=top;i>=0;i--)
		{
			cout<<stack[i]<<" ";
			cout<<endl;
		}
	}
	else{
		cout<<"Stack is empty"<<endl;
	}
}




int main()
{
	cout<<"Enter value of size"<<endl;
	cin>>n;
	int ch,val;
	cout<<"1) To PUSH Data"<<endl;
	cout<<"2) To POP Data"<<endl;
	cout<<"3) To Display Data"<<endl;
	cout<<"4) To Exit Data"<<endl;
	do
	{
		cout<<"Enter choice"<<endl;
		cin>>ch;
		switch(ch) {
         case 1: {
            cout<<"Enter value to be pushed:"<<endl;
            cin>>val;
            push(val);
            break;
         }
         case 2: {
            pop();
            break;
         }
         case 3: {
            display();
            break;
         }
         case 4: {
            cout<<"Exit"<<endl;
            break;
         }
         default: {
            cout<<"Invalid Choice"<<endl;
         }
	}
}	while(ch!=4);
return 0;

}

Leave a Comment