Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following function: void mystery(int num){ int current; stack *stk = (stack *) malloc(sizeof(stack)); queue *que = (queue *) malloc(sizeof(queue)); while( num > 0

Consider the following function:

void mystery(int num){ int current; 
 stack *stk = (stack *) malloc(sizeof(stack)); queue *que = (queue *) malloc(sizeof(queue)); 
 while( num > 0 ){ push(stk, num); num = num - 1; 

}

 while( empty(stk) != 1 ){ current = pop(stk); insert(que, current); printf("%d ", current); 
 } printf(" "); 
 while( empty(que) != 1 ){ current = remove(que); push(stk, current); printf("%d ", current); 
 } printf(" "); 
 while( empty(stk) != 1 ){ current = pop(stk); if( empty(stk) != 1 ){ 
 insert(que, pop(stk)); } 
 printf("%d ", current); } 
 printf(" "); 
 while( empty(que) != 1 ){ current = remove(que); push(stk, current); printf("%d ", current); 

}

 free(stk); free(que); } 

QUESTION 1

  1. Suppose we call mystery(7). What does this print out?

    a.

    1234567

    7654321

    7531

    642

    b.

    1234567

    1234567

    642

    7531

    c.

    1234567

    1234567

    7531

    642

    d.

    7654321

    1234567

    642

    7531

QUESTION 2

Suppose we replace que with a priority queue. In particular assume that the function remove removes and returns the largest element of the queue.

What does a call to mystery(7) print out now?

1234567

7654321

1357

642

1234567

7654321

753

1642

1234567

1234567

1357

642

1234567

1234567

7531

246

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

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions