Question: Description: Please complete the missing code of the stack ADT with an array, such that the program could output a correct answer. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #include #include

Description: Please complete the missing code of the stack ADT with an array, such that the program could output a correct answer.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include  #include  #include  typedef enum {PUSH = 1, POP, END, ERROR_OP} OP; /* the minimum value of interger type. 0x80000000 = -2147483648 * the maximum value of integer type. 0x7fffffff = 2147483647 * They are used to represent the error of stack underflow and stack overflow, respectively */ #define ERROR_UNDERFLOW 0x80000000 #define ERROR_OVERFLOW 0x7fffffff struct stack { int *arr; int capacity; int top; }; typedef struct stack Stack; Stack *create_stack(int capacity) { Stack *S = (Stack *)malloc(sizeof(struct stack)); S->arr = (int*)malloc(sizeof(int) * capacity); S->capacity = capacity; S->top = 0; return S; } void destory_stack(Stack *S) { free(S->arr); free(S); return ; } int is_full(Stack *S) { // write down your code here } int is_empty(Stack *S) { // write down your code here } int push(Stack *S, int x) { if (is_full(S)) { return ERROR_OVERFLOW; } // write down your code here } int pop(Stack *S) { if (is_empty(S)) { return ERROR_UNDERFLOW; } // write down your code here } OP get_op() { char str[20]; scanf("%s", str); if (strcmp(str, "push") == 0) { return PUSH; } else if (strcmp(str, "pop") == 0) { return POP; } else if (strcmp(str, "end") == 0) { return END; } else { return ERROR_OP; } } void print_array(Stack *S) { int i; if (is_empty(S)) { printf("empty "); return ; } while (S->top > 0) { S->top -= 1; int x = S->arr[S->top]; printf("%d ", x); } printf(" "); return ; } int main(void) { int x; int capacity; Stack *S; int flag = 0; int ret = 0; scanf("%d", &capacity); S = create_stack(capacity); while (!flag) { switch(get_op()) { case PUSH: scanf("%d", &x); ret = push(S, x); if (ret == ERROR_OVERFLOW) { printf("stack overflow "); } break; case POP: ret = pop(S); if (ret == ERROR_UNDERFLOW) { printf("stack underflow "); } else { printf("%d ", ret); } break; case END: print_array(S); flag = 1; break; default: printf("error input "); } } destory_stack(S); return 0; }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Input: The first line is a positive integer N, which denotes the capacity of the stack, followed by M lines of stack operations, each chosen from one of the followings: push x, pop. The last line is an ending indicator, end. Output: The top element for each pop operation. If there exists underflow or overflow, please output "stack underflow" or "stack overflow", respectively. The remaining elements in the stack from top to bottom, separated by a space. If the stack is empty, please output empty. Sample Input 1: 4 pop push 1 push 2 end Sample Output 1: stack underflow 2 1 Sample Input 2: 1 push 1 push 2 pop end Sample Output 2: stack overflow 1 empty

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!