Question: Consider the following C code which implements a stack; int data[100]; int sp=0; void push(int n) {data[sp++}=n;} int pop() {return(data{data[--sp]);} For the following questions, show

Consider the following C code which implements a stack;

int data[100];

int sp=0;

void push(int n)

{data[sp++}=n;}

int pop()

{return(data{data[--sp]);}

For the following questions, show the contents of the data array, as well as the value of sp, in the spaces provided (after all the shown instructions are executed). If the contents are unpredictable, indicate this with "?" Assume the statements are executed in order, i.e., the statements in part (b) are executed after the statements in (a) and so on.

(a) Beginning with an empty stack, consider the following statements:

push(10); push(20); push(15);

sp: ____

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

(b) now execute: push(7); pop(); pop();

sp:_______

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

data[4]: ______

(c) Now execute: pop(); push(7); push(8); push(6);

sp:_______

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

data[4]: ______

(d) Now execute: push(15); pop(); pop(); pop(); pop(); pop();

sp:_______

data[0]: ______

data[1]: ______

data[2]:______

data[3]: ______

data[4]: ______

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!