Inserting Nodes After Q: Let's Double Check!

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Nodes
In summary, the author suggests that if we want to insert a node pointed to by P just after node poited to by Q, we have to do the following changes: first change Q->next->prev, and then Q->next.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Smirk)

According to my notes, if we want to insert a node pointed to by P just after node poited to by Q, we have to do the following changements:

$$\begin{pmatrix}
P->prev\\
P->next\\
Q->next\\
Q->next->prev
\end{pmatrix} \leftarrow
\begin{pmatrix}
Q\\
Q->next\\
P\\
P
\end{pmatrix}$$

Shouldn't it be $\rightarrow$ ? :confused:

Also, shouldn't we first change [m]Q->next->prev[/m] and then [m]Q->next[/m]?

Or am I wrong? (Worried)
 
Technology news on Phys.org
  • #2
evinda said:
Hi! (Smirk)

According to my notes, if we want to insert a node pointed to by P just after node poited to by Q, we have to do the following changements:

$$\begin{pmatrix}
P->prev\\
P->next\\
Q->next\\
Q->next->prev
\end{pmatrix} \leftarrow
\begin{pmatrix}
Q\\
Q->next\\
P\\
P
\end{pmatrix}$$

Shouldn't it be $\rightarrow$ ? :confused:

Also, shouldn't we first change [m]Q->next->prev[/m] and then [m]Q->next[/m]?

Or am I wrong? (Worried)

Hey! (Wave)

The symbol $\leftarrow$ means simultaneous assignment.
It means that we do for instance: [m]P->prev = Q;[/m]. (Smirk)

Since the assignment is simultaneous, the order is not relevant.
You may interpret it as if the right hand values are first copied to 4 temporary variables, and then those 4 temporary variables are copied to the left hand variables. (Nerd)
 
  • #3
I like Serena said:
Hey! (Wave)

The symbol $\leftarrow$ means simultaneous assignment.
It means that we do for instance: [m]P->prev = Q;[/m]. (Smirk)

Since the assignment is simultaneous, the order is not relevant.
You may interpret it as if the right hand values are first copied to 4 temporary variables, and then those 4 temporary variables are copied to the left hand variables. (Nerd)

A ok... (Nod) But, when we write an algorithm, we have to change firstly [m]Q->next->prev[/m] and then [m]Q->next[/m], right? (Thinking)
 
  • #4
evinda said:
A ok... (Nod) But, when we write an algorithm, we have to change firstly [m]Q->next->prev[/m] and then [m]Q->next[/m], right? (Thinking)

That is only necessary when you write in an actual programming language like C.
In an "algorithm" you can use this notation for simultaneous assignment. (Nod)
 
  • #5
I like Serena said:
That is only necessary when you write in an actual programming language like C.
In an "algorithm" you can use this notation for simultaneous assignment. (Nod)

I understand... Thank you! (Smile)
 

FAQ: Inserting Nodes After Q: Let's Double Check!

How do I insert a node after Q?

To insert a node after Q, you can use the insertAfter() method in JavaScript. First, select the element after which you want to insert the new node. Then, use the insertAfter() method to insert the new node after the selected element.

What does "Let's Double Check!" mean in this context?

"Let's Double Check!" is a phrase used to indicate that we should review or verify something before moving on to the next step. In this context, it means that we should check our code or actions before inserting a node after Q to ensure that we are doing it correctly.

Can I insert a node after any element?

Yes, you can insert a node after any element using the appropriate method. However, the element must exist in the DOM (Document Object Model) for the insertion to be successful.

Is there a way to insert multiple nodes after Q?

Yes, you can insert multiple nodes after Q by creating an array of nodes and using a loop to insert each node after Q. Alternatively, you can use the insertAdjacentHTML() method to insert multiple nodes at once.

What happens if I try to insert a node after Q but there is no Q element in the DOM?

If there is no Q element in the DOM, the insertion will fail and the new node will not be added. It is important to ensure that the element after which you want to insert the new node exists before attempting to insert the node.

Back
Top