Question #29351

1. Using an array of size 10, implement the PUSH and POP operations of the stack. NB: Let your TOP and BOTTOM be -1 when that stacks is EMPTY.
2. Convert your algorithm above into a c++ code

Expert's answer

#include <iostream>

using namespace std;

typedef int my_type;
#define SZ 10

my_type stack[SZ];
int top = -1, bottom = -1;

bool push(my_type new_el) {
if (top == SZ - 1)
& return false;
stack[++top] = new_el;
return true;
}

my_type pop() {
if (top >= 0)
& return stack[top--];
}

int main()
{
for(;;){
& char q;
& int a;
& cin >> q >> a;
& if (q == '+')
cout << push(a) << endl;
& else if (q == '-')
cout << pop() << endl;
& else cout << top << endl;
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS