DS Tutorial - Solve - 4
DS Tutorial - Solve - 4
Tutorial 3
1. The following figure depicts the initial configuration of a circular doubly linked list
where the head is pointing to the first node.
What would be the output after executing the following sequence of steps -
i) head->next->next->next = head->prev ii) head->prev->prev->prev
= head->next->next->next->prev iii) head->next->next->next->prev
= head->prev->prev->prev iv) printf(“%d” , head->prev->prev->prev-
>data)
then
i) to inserting a node right most ii) to deleting a
node right most iii) to insert a node in the middle
of the list iv) to deleting a node in the middle of
the list
requires how many changes to various next and prev pointers ? Considering the
doubly linked list is not empty and length of more than equal to 2.
if (element->next != NULL){
______blank line________
}
}
Replace the blank line to complete the function ?
Ans: To complete the function, the following code can be added to check and update the
previous pointer of the next element after the newly inserted element: if (element->next !=
NULL){ element->next->prev = element;
}
4. What would be the Prefix notation for the given equation?
K + L - M * N + ( O ^ P ) * W/U/V * T + Q
Ans:
Rules for the conversion of infix to prefix expression:
Q Q
+ + Q
T + QT
* +* QT
V +* QTV
/ +*/ QTV
U +*/ QTVU
/ +*// QTVU
W +*// QTVUW
* +*//* QTVUW
) +*//*) QTVUW
P +*//*) QTVUWP
^ +*//*)^ QTVUWP
O +*//*)^ QTVUWPO
( +*//* QTVUWPO^
+ ++ QTVUWPO^*//*
N ++ QTVUWPO^*//*N
* ++* QTVUWPO^*//*N
M ++* QTVUWPO^*//*NM
- ++- QTVUWPO^*//*NM*
L ++- QTVUWPO^*//*NM*L
+ ++-+ QTVUWPO^*//*NM*L
K ++-+ QTVUWPO^*//*NM*LK
QTVUWPO^*//*NM*LK+-++
The above expression, i.e., QTVUWPO^*//*NM*LK+-++, is not a final expression. We
need to reverse this expression to obtain the prefix expression.
++-+KL*MN*//*^OPWUVTQ
Algorithms:
10. Else
If precedence infix[i] > precedence(stack.top)
Push infix[i] on the top of the stack
else if precedence infix[i] == precedence(stack.top) && infix[i] == '^'
Pop and print the top values of the stack till the condition is true
Push infix[i] into the stack
else if precedence infix[i] == precedence(stack.top)
Push infix[i] on to the stack
Else if precedence infix[i] < precedence(stack.top)
Pop the stack values and print them till the stack is not
empty and infix[i] < precedence(stack.top)
Push infix[i] on to the stack
Ans: The correct Answer is Option d) Implementing a doubly linked list is easier
than a singly linked list is false.
Implementing a doubly linked list requires additional pointers to be maintained for each
Node in the list, which makes it more complex and time-consuming compared to a singly
linked list.