# Question & Answer: Consider the following code. What is printed by the code that follows the function definitions? Pay attention…..

Consider the following code. What is printed by the code that follows the function definitions? Pay attention to when fprintf functions are triggered. function q = enqueue (v, q) q = [v q]: fprintf(‘enqueing %d into queuen’, v): end function [v q] = dequeue(q) v = q(end): fprintf(‘dequeing %d from queuen’, v): q = q(1: end-1): end function s = push(v, s) s = [v s]: fprintf(‘pushing %d onto stackn’, v): end function [v s] = pop(s) v = s(1): fprintf(‘popping %d from stackn, v): s = (2: end): end s1 = [1]: s2 = [2 3]: q = [4 5]: [v s2] = pop(s2) q = enqueue(v, q) [v q] = dequeue(q): s2 = push(v, s2): [v s2] = pop(s2): s1 = push(v, s1) [v q] = dequeue(q): s1 = push(v, s1) [v q] =dequeue(q): s2 = push(v, s2) [v s2] = pop(s2) [v s2] = pop(s2): s1 = push(v, s1) disp(s1): disp(s2): disp(q);

Code;

function q = enqueue(v, q)
q = [v q]; fprintf(‘enqueing %d into queuen’, v);
end

function [v q] = dequeue(q)
v = q(end); fprintf(‘dequeing %d from queuen’, v);
q = q(1:end-1)’
end

function s = push(v, s)
s = [v s]; fprintf(‘pushing %d onto stackn’, v);
end

function [v s] = pop(s)
v = s(1); fprintf(‘popping %d from stackn’, v);
s = s(2:end);
end

s1 = [1];
s2 = [2 3];
q = [4 5];

%v = 2, s2 = [3]
[v s2] = pop(s2);

% q = [5 4 2]
q = enqueue(v, q);

% v=5, q = [4 2]
[v q] = dequeue(q);

% s2 = [5 3]
s2 = push(v, s2);

% v = 5, s2 = [3]
[v s2] = pop(s2);

% s1 = [5 1]
s1 = push(v, s1);

% v = 4, q = [2]
[v q] = dequeue(q);

% s1 = [4 5 1]
s1 = push(v, s1);

% v = 2, q = []
[v q] = dequeue(q);

% s2 = [2 3]
s2 = push(v, s2);

% v = 2, s2 = [3]
[v s2] = pop(s2);

% s1 = [2 4 5 1]
s1 = push(v, s1);

fprintf(“n”);

% 2 4 5 1
disp(s1);
% 3
disp(s2);
% []
disp(q);