0% found this document useful (0 votes)
4 views3 pages

Lab 2 CSD

The document describes a series of operations performed on stacks and queues, detailing the push and pop actions along with the resulting states of the data structures. It includes specific examples with outputs for both stacks and queues, illustrating how elements are added and removed. The final outputs for both data structures are provided, showing the order of elements after all operations.

Uploaded by

caoducanh1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Lab 2 CSD

The document describes a series of operations performed on stacks and queues, detailing the push and pop actions along with the resulting states of the data structures. It includes specific examples with outputs for both stacks and queues, illustrating how elements are added and removed. The final outputs for both data structures are provided, showing the order of elements after all operations.

Uploaded by

caoducanh1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

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]

The while loop pop and print each element

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]

The while loop pops the remaining elements

4
x=4
y=4

3.

EAS*Y*QUE**ST*IO*N***

Steps:

 Push E, A, S → Stack: [E, A, S]


 Pop (S) → Output: S
 Push Y → Stack: [E, A, Y]
 Pop (Y) → Output: Y
 Push Q, U, E → Stack: [E, A, Q, U, E]
 Pop (E) → Output: E
 Pop (U) → Output: U
 Push S → Stack: [E, A, Q, S]
 Push T → Stack: [E, A, Q, S, T]
 Pop (T) → Output: T
 Push I → Stack: [E, A, Q, S, I]
 Push O → Stack: [E, A, Q, S, I, O]
 Pop (O) → Output: O
 Push N → Stack: [E, A, Q, S, I, N]
 Pop (N) → Output: N
 Pop (I) → Output: I

S, Y, E, U, T, O, N, I

4.

Steps:

 Push L, A → Stack: [L, A]


 Pop (A) → Stack: [L]
 Push S, T, I → Stack: [L, S, T, I]
 Pop (I) → Stack: [L, S, T]
 Push N → Stack: [L, S, T, N]
 Pop (N) → Stack: [L, S, T]
 Push F, I, R → Stack: [L, S, T, F, I, R]
 Pop (R) → Stack: [L, S, T, F, I]
 Pop (I) → Stack: [L, S, T, F]
 Push S, T → Stack: [L, S, T, F, S, T]
 Pop (T) → Stack: [L, S, T, F, S]
 Push O → Stack: [L, S, T, F, S, O]
 Push U → Stack: [L, S, T, F, S, O, U]
 Pop (U) → Stack: [L, S, T, F, S, O]
 Pop (O) → Stack: [L, S, T, F, S]
 Pop (S) → Stack: [L, S, T, F]

[L, S, T, F]

s[0] = L, s[1] = S, s[2] = T, s[3] = 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:

 Enqueue E, A, S → Queue: [E, A, S, -, -]


 Dequeue (E) → Queue: [-, A, S, -, -]
 Enqueue Y → Queue: [-, A, S, Y, -]
 Dequeue (A) → Queue: [-, -, S, Y, -]
 Enqueue Q, U → Queue: [Q, U, S, Y, -]
 Enqueue E → Queue: [Q, U, S, Y, E]
 Dequeue (S) → Queue: [Q, U, -, Y, E]
 Dequeue (Y) → Queue: [Q, U, -, -, E]
 Enqueue S, T → Queue: [Q, U, S, T, E]
 Dequeue (Q) → Queue: [-, U, S, T, E]
 Dequeue (U) → Queue: [-, -, S, T, E]
 Enqueue I, O → Queue: [I, O, S, T, E]
 Dequeue (S) → Queue: [I, O, -, T, E]
 Enqueue N → Queue: [I, O, N, T, E]

q[0] = I, q[1] = O, q[2] = N, q[3] = T, q[4] = E

You might also like