Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Time limit: 5000ms Memory limit: 256mb Description: Please complete the missing code of the stack ADT with a linked list, such that the program could

Time limit: 5000ms Memory limit: 256mb Description: Please complete the missing code of the stack ADT with a linked list, such that the program could output a correct answer. -------------------------Copy the following code, complete it and submit-------------------------  #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 typedef struct node Node; typedef struct node Node; struct node { char v; Node *next; Node *prev; }; typedef struct linked_list List; struct linked_list { Node *head; Node *tail; }; // insert an element 'x' at the back of the LinkedList void insert(List *L, char x) { Node *node = (Node *)malloc(sizeof(struct node)); node->v = x; node->prev = L->tail->prev; node->prev->next = node; node->next = L->tail; L->tail->prev = node; } // delete the node p void delete(List *L, Node *p) { Node *pred = p->prev; Node *succ = p->next; pred->next = succ; succ->prev = pred; free(p); } // create a empty stack List *create_stack() { List *L; L = (List *)malloc(sizeof(struct linked_list)); L->head = (Node *)malloc(sizeof(struct node)); L->head->prev = NULL; L->tail = (Node *)malloc(sizeof(struct node)); L->head->next = L->tail; L->tail->prev = L->head; L->tail->next = NULL; return L; } // destroy a stack and release its nodes void destroy_stack(List *L) { Node *curr = L->head->next; while (curr != L->tail) { Node *next = curr->next; free(curr); curr = next; } free(L->head); free(L->tail); free(L); return; } int is_full(List *L) { // In C, zero is used to represent False } int is_empty(List *L) { // write down your code here } int push(List *L, char x) { if (is_full(L)) { return ERROR_OVERFLOW; } // write down your code here } int pop(List *L) { Node *p; char v; if (is_empty(L)) { return ERROR_UNDERFLOW; } // write down your code here } // decode the input operations 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; } } // display the elements in the stack from top to bottom void print_stack(List *L) { Node *ptr = L->tail->prev; if (ptr == L->head) { printf("empty "); return; } while (ptr != L->head) { printf("%c ", ptr->v); ptr = ptr->prev; } printf(" "); return; } int main(void) { char x; List *L = NULL; int flag = 0; L = create_stack(); int ret = 0; while (!flag) { switch (get_op()) { case PUSH: scanf(" %c", &x); ret = push(L, x); if (ret == ERROR_OVERFLOW) { printf("stack overflow "); } break; case POP: ret = pop(L); if (ret == ERROR_UNDERFLOW) { printf("stack underflow "); } else { printf("%c ", (char)ret); } break; case END: print_stack(L); flag = 1; break; default: printf("error input "); } } destroy_stack(L); return 0; }
-------------------------------------------End of Code------------------------------------------- Input: 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 per 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: pop push a push b end Sample Output 1: stack underflow b a Sample Input 2: push a pop end Sample Output 2: a empty

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago

Question

What did they do? What did they say?

Answered: 1 week ago