0% found this document useful (0 votes)
3 views

Assignment-2 Solutions Data Structures

The document provides solutions for an assignment on data structures, covering topics such as stack operations, postfix expression conversion, recursion with the Tower of Hanoi, and queue operations using a circular array. It includes step-by-step processes for converting an infix expression to postfix and detailed pseudocode for queue operations. The final results include a postfix expression, a final stack state, and a recursive tree representation for the Tower of Hanoi.

Uploaded by

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

Assignment-2 Solutions Data Structures

The document provides solutions for an assignment on data structures, covering topics such as stack operations, postfix expression conversion, recursion with the Tower of Hanoi, and queue operations using a circular array. It includes step-by-step processes for converting an infix expression to postfix and detailed pseudocode for queue operations. The final results include a postfix expression, a final stack state, and a recursive tree representation for the Tower of Hanoi.

Uploaded by

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

Assignment-2 Solutions: Data Structures

Section-I: Stack

Q1 Solution:

Given Infix Expression: A + ((B + C) + (D + E) * F) / G

Step-by-step conversion to Postfix using stack:

Final Postfix Expression: A B C + D E + F * + G / +

Refer to the detailed steps in your notes.

Q2 Solution:

Initial Stack:

TOP = 5

STACK: [5, 2, 3, 10, 7, __, __, __]

After each operation:

1. ITEMA = POP(STACK) -> ITEMA = 7, STACK = [5, 2, 3, 10, __, __, __, __]

2. ITEMB = POP(STACK) -> ITEMB = 10, STACK = [5, 2, 3, __, __, __, __, __]

3. PUSH(STACK, ITEMB + 2) -> STACK = [5, 2, 3, 12, __, __, __, __]

4. PUSH(STACK, 8) -> STACK = [5, 2, 3, 12, 8, __, __, __]

5. PUSH(STACK, ITEMA + ITEMB) -> STACK = [5, 2, 3, 12, 8, 17, __, __]

Final Stack: [5, 2, 3, 12, 8, 17, __, __]

Section-II: Recursion & Queue


Q1 Solution:

Recursive Tree for Tower of Hanoi (n = 3):

toh(3)

/ \

toh(2) toh(2)

/ \ / \

toh(1) toh(1) toh(1) toh(1)

Total Moves = 7 (Calculated using 2^3 - 1).

Q2 Solution:

Pseudocode for Queue Operations (Circular Array):

1. Inserting an Element:

function enqueue(queue, element):

if (rear + 1) % size == front:

print("Queue is Full")

else:

rear = (rear + 1) % size

queue[rear] = element

2. Deleting an Element:

function dequeue(queue):

if front == rear:

print("Queue is Empty")

else:
front = (front + 1) % size

return queue[front]

3. Counting Total Elements:

function count(queue):

return (rear - front + size) % size

You might also like