Answer to Question #29351 in C++ for curtis tandoh

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
1
Expert's answer
2013-04-30T09:08:43-0400
#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!

Comments

Assignment Expert
01.05.13, 14:37

You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!

curtis
01.05.13, 00:02

thanks guys. i really appreciate this. it was cool

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS