Lab 2 CSD
Lab 2 CSD
x = 3; y = 5; z = 2;
s.makeEmpty(); // Make the stack empty
s.push(x); // Push 3 onto the stack
s.push(4); // Push 4 onto the stack
s.pop(); // Pop the top of the stack (4)
s.push(y); // Push 5 onto the stack
s.push(3); // Push 3 onto the stack
s.push(z); // Push 2 onto the stack
s.pop(); // Pop the top of the stack (2)
s.push(2); // Push 2 onto the stack
s.push(x); // Push 3 onto the stack
[3, 5, 3, 2, 3]
3 2 3 5 3
2.
x = 3; y = 1;
s.makeEmpty(); // Make the stack empty
s.push(5); // Push 5 onto the stack
s.push(7); // Push 7 onto the stack
s.pop(); // Pop the top (7)
x += y; // x becomes 4 (x = 3 + 1)
s.pop(); // Pop the top (5)
s.push(x); // Push 4 onto the stack
s.push(y); // Push 1 onto the stack
s.push(2); // Push 2 onto the stack
s.pop(); // Pop the top (2)
s.pop(); // Pop the top (1)
[4]
4
x=4
y=4
3.
EAS*Y*QUE**ST*IO*N***
Steps:
S, Y, E, U, T, O, N, I
4.
Steps:
[L, S, T, F]
5.
EAS*Y*QUE**ST**IO*N**
Steps:
Enqueue E, A, S → Queue: [E, A, S]
Dequeue (E) → Output: E
Enqueue Y → Queue: [A, S, Y]
Dequeue (A) → Output: A
Enqueue Q, U, E → Queue: [S, Y, Q, U, E]
Dequeue (S) → Output: S
Dequeue (Y) → Output: Y
Enqueue S, T → Queue: [Q, U, E, S, T]
Dequeue (Q) → Output: Q
Dequeue (U) → Output: U
Enqueue I, O → Queue: [E, S, T, I, O]
Dequeue (E) → Output: E
Enqueue N → Queue: [S, T, I, O, N]
Dequeue (S) → Output: S
Dequeue (T) → Output: T
Dequeue (N) → Output: N
E, A, S, Y, E, U, S, T, N
6.
EAS*Y*QUE*ST**IO*N**
Steps: