0% found this document useful (0 votes)
43 views10 pages

DS Tutorial - Solve - 4

To insert or delete a node from the rightmost position in a doubly linked list requires changes to 2 pointers - next and prev. To insert or delete a node

Uploaded by

V. Kethareswaran
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)
43 views10 pages

DS Tutorial - Solve - 4

To insert or delete a node from the rightmost position in a doubly linked list requires changes to 2 pointers - next and prev. To insert or delete a node

Uploaded by

V. Kethareswaran
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/ 10

Data Structure Lab, B.

Tech 2nd Semester

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)

Ans: The circular doubly linked list is given as follows

i) head->next->next->next = head->prev sets the next pointer of node 3 to point to


the prev pointer of node 1, which is the last node.
So, after step 1, the linked list becomes:

ii) head->prev->prev->prev = head->next->next->next->prev sets the prev pointer


of node 4 (which is 2 positions behind head) to the prev pointer of node
5.

So, after step 2, the linked list becomes:

iii) head->next->next->next->prev = head->prev->prev->prev sets the prev pointer


of node 4 to point to the prev pointer of node 1.
So, after step 2, the linked list becomes:

iv) printf("%d", head->prev->prev->prev->data) prints the value stored in the data


field of node 4, which is 4.

So, the final output will be 4.


2. If a doubly linked list is declared as

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.

Ans: 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

3. The following C function inserts a new element after a specific element ( a


pointer is given which points to that specific element after which the new
element needs to be inserted) in a doubly linked list.

struct Node { int


data; struct
Node* next;
struct Node*
prev;
};

void insertAfter(struct Node* previous, int value) { struct Node*


element = (struct Node*)malloc(sizeof(struct Node)); element-
>data = value; element->next = previous->next; previous->next =
element; element->prev = previous;

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:

o First, reverse the infix expression given in the problem.


o Scan the expression from left to right.
o Whenever the operands arrive, print them.
o If the operator arrives and the stack is found to be empty, then simply push the
operator into the stack.
o If the incoming operator has higher precedence than the TOP of the stack, push
the incoming operator into the stack.
o If the incoming operator has the same precedence with a TOP of the stack, push
the incoming operator into the stack.
o If the incoming operator has lower precedence than the TOP of the stack, pop,
and print the top of the stack. Test the incoming operator against the top of the
stack again and pop the operator from the stack till it finds the operator of a
lower precedence or same precedence.
o If the incoming operator has the same precedence with the top of the stack and
the incoming operator is ^, then pop the top of the stack till the condition is true.
If the condition is not true, push the ^ operator.
o When we reach the end of the expression, pop, and print all the operators from
the top of the stack.
o If the operator is ')', then push it into the stack.
o If the operator is '(', then pop all the operators from the stack till it finds )
opening bracket in the stack.
o If the top of the stack is ')', push the operator on the stack.
o At the end, reverse the output.

o Input expression Stack 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.

Final expression will be:

++-+KL*MN*//*^OPWUVTQ

Algorithms:

1. REVERSE the expression


2. Create Stack,Prefix
3. SET infix = reverse(infix)
4. loop i = 0 to length of expression
5. if infix[i] is operand
SET prefix= prefix + infix[i]

6. else if infix[i] is '('


push (infix[i]) to stack

7. else if infix[i] is ')'


pop and print the values of stack till the symbol ')' is not found

8. else if infix[i] is an operator(+, -, *, /, ^) then


9. if the stack is empty then
push infix[i] on the top of the stack.

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

11. End loop


12. Pop and print the remaining elements of the stack
13. Prefix final = reverse(prefix)
14. return

5. Which of the following is false about a doubly linked list?


a) We can navigate in both the directions
b) It requires more space than a singly linked list
c) The insertion and deletion of a node take a bit longer
d) Implementing a doubly linked list is easier than singly linked list

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.

You might also like