using namespace std;
void push(int);
void pop();
void view();
int top = -1;
int stack[5];
void main_of_stack();
void main_of_stack()
{
int w = 1;
while (w == 1)
{
int ch;cout << "Enter your choice" << endl;
cout << "1) push " << endl;
cout << "2) pop " << endl;
cout << "3) view" << endl;
cout << "4) Exit" << endl;
cin >> ch;
switch (ch)
{
case 1:
int n;
cout << "enter value you want to insert" << endl;
cin >> n;
push(n);
break;
case 2:
pop();
break;
case 3:
view();
break;
case 4:
w++;
break;
default:
cout << "invalid input" << endl;
}
}
}
void main()
{
main_of_stack();
}
void push(int x)
{
cout << "Push the value" << endl;
if (top >= 5)
{
cout << "Stack is full or over flow" << endl;
}
else
{
stack[top] = x;
top++;
}
}
void pop()
{
if (top == -1)
{
cout << "stack is empty or overflow" << endl;
}
else
{
top--;
}
}
void view()
{
if (top == -1)
{
cout << "empty stck" << endl;
}
else{
int cho;
cout << "1)top to bottom" << endl;
cout << "2)bottom to top" << endl;
cin >> cho;
switch (cho)
{
case 1:
for (int i = top; i >= 0; i--)
{
cout << stack[i] << endl;
}
break;
case 2:
for (int i = 0; i <= top; i++)
{
cout << stack[i] << endl;
}
break;
default:
main_of_stack();
}
}
}
No comments:
Post a Comment