Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

It currently doesnt work, so fix it. This is what the output should look like: Push 13 Push 15 Push 11 Top = 11, Min=11

image text in transcribedIt currently doesnt work, so fix it. This is what the output should look like: Push 13 Push 15 Push 11 Top = 11, Min=11 Pop Top = 15, Min=13 Pop Top = 13, Min=13 Push 20 Push 10 Push 9 Push 17 Push 18 Top = 18, Min=9 Pop Top = 17, Min=9 Pop Top = 9, Min=9 Pop Top = 10, Min=10 Pop Top = 20, Min=13 Pop Top = 13, Min=13

#include  #include  struct Node { int val; struct Node *next; }; typedef struct Node *Min_Stack; Min_Stack create_minstack() { return NULL; } Min_Stack destroy_minstack(Min_Stack s) { struct Node *p = s; while (p!=NULL) { struct Node *temp = p; p=p->next; free(temp); } } int get_top(Min_Stack stack) { if (stack==NULL) { return -1; } return stack->val; } int get_min(Min_Stack stack) { return 0; } int is_empty(Min_Stack stack) { if (stack==NULL) { return 1; } return 0; } Min_Stack pop(Min_Stack stack) { if (is_empty(stack)) { return NULL; } struct Node * p = stack->next; free(stack); return p; } Min_Stack push(Min_Stack stack, int val) { struct Node *p = (struct Node *) malloc(sizeof(struct Node)); p->val = val; p->next = stack; return p; } void main() { Min_Stack s = create_minstack(); s = push(s, 13); printf("Push 13 "); s = push(s, 15); printf("Push 15 "); s = push(s, 11); printf("Push 11 "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = pop(s); printf("Pop "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = pop(s); printf("Pop "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = push(s, 20); printf("Push 20 "); s = push(s, 10); printf("Push 10 "); s = push(s, 9); printf("Push 9 "); s = push(s, 17); printf("Push 17 "); s = push(s, 18); printf("Push 18 "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = pop(s); printf("Pop "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = pop(s); printf("Pop "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = pop(s); printf("Pop "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = pop(s); printf("Pop "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); s = pop(s); printf("Pop "); printf("Top = %d, Min=%d ",get_top(s),get_min(s)); destroy_minstack(s); return; } 
Problem 3 (2 pts). Attached is a program minstack.c that has a "min stack", which stores positive integers. Such a stack has the following operations: - Create a minstack - Destroy a minstack - Push an integer value into the minstack - Pop an integer value off the minstack - Get the top integer from the minstack - Get the minimum integer value in the minstack

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions