Question: Create a stack program where the stack array (and top) are passed as parameters to the helper functions. #include // stardard input/output library #include //
Create a stack program where the stack array (and top) are passed as parameters to the helper functions.
|
#include // stardard input/output library |
| #include // standard boolean library: bool, true, false |
| #define MAXSIZE 100 |
| int stack1[MAXSIZE]; // array in which stack will live |
| int top1 = -1; // top valid location in stack, -1 == empty |
| int stack2[MAXSIZE]; // array in which stack will live |
| int top2 = -1; // top valid location in stack, -1 == empty |
| bool isEmpty (int* s, int t) { |
| // returns true if t = -1 |
| // INSERT YOUR CODE HERE |
| } |
| bool isFull (int* s, int t) { |
| // returns true if no more room in the stack |
| // INSERT YOUR CODE HERE |
| } |
| void push(int v, int* s, int* tp) { |
| // put v onto the top of the stack s unless it is already full |
| // update s and *tp -- requires top to be passed by reference! |
| // INSERT YOUR CODE HERE |
| } |
| int pop (int* s, int* tp) { |
| // return the top entry in the stack unless stack is empty |
| // update s and *tp -- requires top to be passed by reference! |
| // INSERT YOUR CODE HERE |
| } |
| int main () { |
| printf("pushing: 1, 2, 3, 4, 5 onto first stack "); |
| printf("pushing: 100, 200, 300, 400, 500 onto second stack "); |
| push(1, stack1, &top1); |
| push(2, stack1, &top1); |
| push(3, stack1, &top1); |
| push(4, stack1, &top1); |
| push(5, stack1, &top1); |
| push(100, stack2, &top2); |
| push(200, stack2, &top2); |
| push(300, stack2, &top2); |
| push(400, stack2, &top2); |
| push(500, stack2, &top2); |
| printf("popping alternating stacks: "); |
| printf("1> %d ",pop(stack1, &top1)); |
| printf("2> %d ",pop(stack2, &top2)); |
| printf("1> %d ",pop(stack1, &top1)); |
| printf("2> %d ",pop(stack2, &top2)); |
| printf("1> %d ",pop(stack1, &top1)); |
| printf("2> %d ",pop(stack2, &top2)); |
| printf("1> %d ",pop(stack1, &top1)); |
| printf("2> %d ",pop(stack2, &top2)); |
| printf("1> %d ",pop(stack1, &top1)); |
| printf("2> %d ",pop(stack2, &top2)); |
| printf("1> %d ",pop(stack1, &top1)); |
| printf("2> %d ",pop(stack2, &top2)); |
| return 0; |
| } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
