written 8.6 years ago by
yashbeer
★ 11k
|
|
Program:
#include<iostream>
#include<conio.h>
using namespace std;
class Stack
{
int stk[10];
int top;
public:
Stack()
{
top=-1;
}
//This funtion is used to push an elemnt into stack
void push(int x)
{
if(isFull())
{
cout <<"Stack is full";
return;
}
top++;
stk[top]=x;
cout <<"Inserted Element:"<<" "<<x;
}
//This functionn is used to check if the stack is full
bool isFull()
{
int size=sizeof(stk)/sizeof(*stk);
if(top == size-1)
{
return true;
}
else
{
return false;
}
}
//This function is used to check if the stack is empty
bool isEmpty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
//This function is used to pop an element of stack
void pop()
{
if(isEmpty())
{
cout <<"Stack is Empty";
return;
}
cout <<"Deleted element is:" <<" "<<stk[top--];
}
//This function is used to display the elements of the stack
void display()
{
if(top==-1)
{
cout <<" Stack is Empty!!";
return;
}
for(int i=top;i>=0;i--)
{
cout <<stk[i] <<" ";
}
}
};
void main()
{
int ch;
Stack st;
while(1)
{
cout <<"\n1.Push 2.Pop 3.Display 4.Exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"Enter the element you want to push";
cin >> ch;
st.push(ch);
break;
case 2: st.pop();
break;
case 3: st.display();
break;
case 4: exit(0);
}
}
}
Sample Output:
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 1
Enter the element you want to push 11
Inserted Element: 11
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 1
Enter the element you want to push 22
Inserted Element: 22
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 1
Enter the element you want to push 33
Inserted Element: 33
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 1
Enter the element you want to push 44
Inserted Element: 44
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 1
Enter the element you want to push 55
Inserted Element: 55
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 3
55 44 33 22 11
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 2
Deleted element is: 55
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice 3
44 33 22 11
1.Push 2.Pop 3.Display 4.Exit
Enter ur choice