Home > Back-end >  C language dynamic linked list
C language dynamic linked list

Time:10-09

To ask who the second image wavy lines what is the function of a part of the statement

CodePudding user response:

Assume that p - & gt; Next - & gt; H1 to h1
It s - & gt; After the next=p.n ext, s - & gt; Next, & gt; H1 s next to h1, it ok

Then p - & gt; Next=s; Becomes a p - & gt; Next, & gt; S p s next orientation, so the results into a p - & gt; Next, & gt; S - & gt; Next, & gt; H1
If there is no s - & gt; Next=p.n ext, h1 is I can't find it, because the p - & gt; Next=s yes p next abandoned the h1, points to the s, so no h1 node just want it, h1 node is lost? So keep up to the h1 first and then how to keep? Keep in s next directly, so is the s - & gt; Next=p.n ext

Actually using a temporary variable, it is estimated that LZ is understand
The Node * h1=p - & gt; Next;//keep the h1 next point to the node p
P - & gt; Next=s;//and then the next point to p s
S - & gt; Next=h1;//and then s next to the h1
This also reached the p - & gt; Next, & gt; S - & gt; Next, & gt; H1

So the s - & gt; Next=p.n ext just omit the Node * h1=p - & gt; Next use temporary variable




CodePudding user response:

reference 1st floor qybao response:
assume that p - & gt; Next - & gt; H1 to h1
It s - & gt; After the next=p.n ext, s - & gt; Next, & gt; H1 s next to h1, it ok

Then p - & gt; Next=s; Becomes a p - & gt; Next, & gt; S p s next orientation, so the results into a p - & gt; Next, & gt; S - & gt; Next, & gt; H1
If there is no s - & gt; Next=p.n ext, h1 is I can't find it, because the p - & gt; Next=s yes p next abandoned the h1, points to the s, so no h1 node just want it, h1 node is lost? So keep up to the h1 first and then how to keep? Keep in s next directly, so is the s - & gt; Next=p.n ext

Actually using a temporary variable, it is estimated that LZ is understand
The Node * h1=p - & gt; Next;//keep the h1 next point to the node p
P - & gt; Next=s;//and then the next point to p s
S - & gt; Next=h1;//and then s next to the h1
This also reached the p - & gt; Next, & gt; S - & gt; Next, & gt; H1

So the s - & gt; Next=p.n ext just omit the Node * h1=p - & gt; Next use temporary variable

That is to say the entire list is by p - & gt; Next this address to connect to p is always the last one node, the p - p & gt; Next temporarily on the new connection of s next inside a unit and move back towards s and then the p - & gt; To recover next is such a cycle right

CodePudding user response:

For the s - & gt; Next assignment, the code is the tail insertion method, namely the new nodes are inserted, in from the tail of the new node next with p - & gt; Next assignment,

In fact, this statement can be removed, because outside the loop has p - & gt; Next=NULL; The end of such guarantee singly linked list is NULL,
  • Related