Supply appropriate definitions for push() and pop() functions and auxiliary exte
ID: 3611172 • Letter: S
Question
Supply appropriate definitions for push() and pop() functionsand auxiliary external variable definitions, so that push() andpop() implement the Stack ADT operations using an array and thefollowing main function works. (You may assume that at every pointin the execution there will never have been more than 10 morepush()'es than pop()'s).
int main() {
char s[6];
int x;
while(scanf("%5s", s) == 1) {
if(isdigit(s))
push(atoi(s));
else {
switch(s[0]) {
case '+': push(pop() + pop()); break;
case '*': push(pop() * pop()); break;
case '-':
x = pop();
push(x-pop());
break;
case '/':
x= pop();
push(x-pop());
break;
default:
return;
}
}
}
}
Explanation / Answer
Here you go. #include // The array will store the items in the stack, first in// index 0, then 1, etc. top will represent the index// to the top element in the stack. If the stack is // empty top will be -1.#define SIZE 10#define EMPTY -1struct stack {int items[SIZE];int top; };void initialize(struct stack* stackPtr);int full(struct stack* stackPtr);int push(struct stack* stackPtr, int value);int empty(struct stack* stackPtr);int pop(struct stack* stackPtr);int top(struct stack* stackPtr);int main() {int i;struct stack mine;// Set up the stack and push a couple items, then pop one.initialize(&mine);push(&mine, 4);push(&mine, 5);printf("Popping %d ", pop(&mine));// Push a couple more and test top.push(&mine, 22);push(&mine, 16);printf("At top now = %d ", top(&mine));// Pop all three off.printf("Popping %d ", pop(&mine));printf("Popping %d ", pop(&mine));printf("Popping %d ", pop(&mine));// Checking the empty function.if (empty(&mine))printf("The stack is empty as expected. ");// Fill the stack.for (i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.