867 views
0 votes
0 votes

What does the following routine do on doubly linked list. M is some middle node address and N is a newly inserted node address.

N → lptr = M ;

N → rtptr = M → rptr ;

N → lptr → rptr = N ;

N → rptr → lptr = N ;

Inserts a node to

  1. Right of M
  2. Left of M
  3. Right Most
  4. Left Most

1 Answer

4 votes
4 votes

Here it is just inserting a new node $N$ in right of $M$.

as it is a Doubly Linked List , so it has two pointers , one pointing to the previous node $(Lptr)$ and the other point to the next node$(Rptr)$.

Lightbox

in this case ,

$N → lptr = M ;$

The previous pointer of $N$ is pointed to $M$.

$N → rtptr = M → rptr ;$

Now, the next pointer of $N$ is pointed to that node to which the next pointer of $M$ was pointing .

$N → lptr → rptr = N ;$

The next pointer of $M$ is pointing to $N$ ( as the previous pointer of $N$ is  nothing but $M$)

$N → rptr → lptr = N ;$

The , previous pointer of next node of $N$ is now pointing to $N$.

 

Related questions

0 votes
0 votes
1 answer
1
Abhrajyoti00 asked Oct 29, 2022
745 views
Can there be “Stack Overflow” in Linked list Implementation of stack? If Yes, how?
2 votes
2 votes
1 answer
2
Mk Utkarsh asked Dec 5, 2018
1,063 views
int find (struct node * first, int n) { while (first data ! = n) first = first — next; if (first data = = n) return(1); else return (-1); in the above code segment if ...
0 votes
0 votes
1 answer
4