#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//Stack
//www.softafzar.net
const int maxsize = 4; //4 is maximum size of our stack
int top = - 1;
int stack[maxsize];
void push(int x)
{
stack[++top] = x;
}
void pop(int &x)
{
if (top <= - 1)
cout << "Stack is empty";
else
{
cout << "\n" << x << " is deleted";
x = stack[--top];
}
}
void main()
{
int x, i;
char option;
while (1)
{
cout << "\n\n\t1)push";
cout << "\n\t2)pop";
cout << "\n\t3)display";
cout << "\n\t4)exit";
cout << "\n\tEnter your option: ";
option = getch();
switch (option)
{
case '1':
if (top >= maxsize)
{
cout << "Stack is Full";
}
else
{
cout << "\n Enter x:";
cin >> x;
push(x);
}
break;
case '2':
pop(x);
break;
case '3':
cout << "\n";
for (i = top; i >= 0; i--)
cout << stack[i] << "";
break;
case '4':
exit(1);
}
}
}
موفق باشید.