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

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Nodes
AI Thread Summary
To insert a node pointed to by P after a node pointed to by Q, simultaneous assignment notation is used, indicated by the symbol $\leftarrow$. This means that the assignments occur at the same time, making the order of operations irrelevant in this context. However, when implementing the algorithm in a programming language like C, it is important to first update Q->next->prev before changing Q->next. This distinction is crucial for correct implementation in code versus theoretical algorithm notation. Understanding this difference clarifies the insertion process in linked list operations.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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)
 
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)
 
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)
 
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)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top