How to use tail interpolation to build a double linked list (C language, acyclic)

How to use tail interpolation to build doubly linked list

In fact, I originally wanted to complete the quick list of the linked list given by Wang team, but after I finished the establishment of the double-linked list, it was 12 o'clock, and I had to get up early QAQ tomorrow. I was dead.

One, why do you have a double linked list

Let me talk about it single-chain
single list so long

 

 

One of his node structures is

[Element field | next pointer field]

Each time the post-insertion method is used to establish a singly linked list, the pointer field in the previous node will point to the next node, such as the first node in the above figure (the node where the e1 element is located, the blue shaded node is Head node) pointer field points to the next node

And so on

We can get a linked list linked by pointers, which is called a single linked list

We can see that if I have a pointer field of a node where an element is located, I can easily insert the node around the node where the element is located

but! ! ! If I want to interpolate before the previous element of this element, I have to traverse the table again from the beginning, which is a waste of time (that is, I can't turn around while driving on a single line)

So we introduced the doubly linked list

Second, the establishment of doubly linked lists

 

The length of the double linked list is as follows

 

 

 

 

 One of his node structures is

[Prior pointer field | element field | next pointer field]

The double linked list has one more "prior pointer field" than the above single linked list. Prior is a needle pointing to the previous node, and next is the same as the above single linked list to point to a node pointer at the back

Back to the question above

If we have to pre-insert the element before an element, it will be much faster to use the double linked list

Because we no longer need to traverse the entire table from the beginning, the address of the previous node stored in each node of the double linked list

We only need to move the pointer forward to complete (single line becomes double line dashed line! You can turn around at any time)

Here are the steps on how to build a doubly linked list

 

 

1. The definition of node type of doubly linked list

1 typedef struct DNode{
2     int data;
3     struct DNode *prior, *next;
4 }DNode, *DLinkList; 

An element field is defined (the int type I use here, which can be replaced), and two pointer fields * prior and * next, which are used to point to the previous node and the next node of the node respectively

 

2. Establishment of doubly linked list

 

1 DLinkList DList_Create (DLinkList & D)
 2  {
 3      int num;
 4      scanf ( " % d " , & num);
 5      D = (DNode *) malloc ( sizeof (DNode)); // define the head node 
6      DNode * S, * P = D; // S is a pointer to the node to be inserted 
. 7      p-> Next = NULL;
 . 8      the while - (NUM =! . 1 ) // input stop -1 
. 9      {
 10          S = (DNode *) malloc ( sizeof (DNode));// Apply space for s to be inserted 
11          s-> data = num;
 12          s-> next = p-> next; // s next copies the next field of the previous node p 
13          p-> next = s; // The next point of the previous node p points to s 
14          s-> prior = p; // The priority of s points to the previous node p 
15          p = p-> next; // The p pointer moves backward by 
16          scanf ( " % d " , & num); // The value of the next element 
17      }
 18      return D;
 19 }

 

 

The steps in while are as follows

s = (DNode *) malloc ( sizeof (DNode)); // Apply space for the s to be inserted

 

s-> next = p-> next; // The next field of s copies the next field of the previous node p

 

 

 

p-> next = s; // next point of the previous node p points to s

 

 

 

s-> prior = p; // priority of s points to the previous node p

 

p = p-> next; // p pointer moves back one bit

 

 I am 1.22 now and I will die suddenly if I don't sleep again.

Hehe

 

Guess you like

Origin www.cnblogs.com/luoyoucode/p/12677428.html