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
Get step-by-step solutions from verified subject matter experts
