Previously exposed to the linked list has only one pointer, pointing to the direct successor, the entire list can only be accessed from the table header in one direction to the end of the table, the structure of the list is collectively referred to as "one-way linked list" or "single linked list."
If the algorithm needs to find a node's forward node frequently, the single-linked list can be solved by traversing the whole list, increasing the time complexity of the algorithm and affecting the overall efficiency.
In order to solve these problems quickly and conveniently, a pointer variable is provided for each node on the basis of the unidirectional list, which is used to point to the direct forward element of each node. Such lists are referred to as "doubly linked lists" or "doubly linked lists".
Nodes in a doubly linked list
The nodes in a doubly linked list have two pointer fields, one pointing directly to the front, and one pointing directly to the successor. (The forward node of the first node in the list is null, and the subsequent node of the last node is null)
Figure 1 nodes in a doubly linked list
The specific composition of the node:
struct Line
{ struct line *prior; // pointing directly to the preceding trend int data; struct// Point Direct successor }line;
Create a doubly linked list and initialize it
In the process of creating a doubly linked list, each node needs to initialize the data field and two pointer fields, one point to the direct forward node, and the other to the direct successor node.
For example, create a doubly linked list line (round-the-line):
Figure 2 Bidirectional table (All-in-two)
Implementation code:
line* Initline (Line *head)
{Head= (line*)malloc(sizeof(line));//Create the first node of the list (first element node) .Head->prior =NULL; Head->next =NULL; Head->data =1; Line*list =Head; for(intI=2; i<=3; i++)
{//Create and initialize a new nodeLine *body = (line*)malloc(sizeof(line)); Body->prior =NULL; Body->next =NULL; Body->data =i; List->next = body;//the next pointer to the direct forward node points to the new node .Body->prior = list;//The new node points to the direct forward node.List = List->Next; }
returnhead;}
Inserting nodes in a doubly linked list
For example, insert a node 4 into (1,4,2,3).
Realize:
Figure 3 Insert node 4 when inserting data in a doubly linked list, the first two-step operation labeled 1 in Figure 3 is completed, and then the two-step operation labeled 2 is finished, and if 2 is completed first, the Node 2 cannot be accessed through the head pointer, and additional pointers are required, although it can be achieved but is more troublesome than the previous one.
Implementation code:
Line *insertline (line * head,intDataintadd)
{//Create a new data-domain nodeLine * temp = (line*)malloc(sizeof(line)); Temp->data =data; Temp->prior =NULL; Temp->next =NULL; //INSERT into the list header, special consideration if(Add = =1)
{Temp->next =Head; Head->prior =temp; Head=temp; }
Else
{ Line*body =Head; //Locate the previous node where you want to insert the location for(intI=1; i<add-1; i++)
{Body= body->Next; }
//Judging the condition is true, indicating that the insertion position is the end of the chain if(Body->next = =NULL)
{Body->next =temp; Temp->prior=Body }
Else
{Body->next->prior =temp; Temp->next = body->Next; Body->next =temp; Temp->prior =body; } }
returnhead;}
Delete a node in a doubly linked list
When a doubly linked list deletes a node, it traverses the list directly, finds the node to be deleted, and then uses the two pointer fields of that node to complete the delete operation.
For example, delete node 2 in (1,4,2,3):
//Delete node function, data is the value of the field to delete the nodeLine *delline (line *head,intdata)
{ Line*temp =Head; //traversing a linked list while(temp)
{//determines whether the data field in the current node is equal or not, and if it is equal, remove the node. if(Temp->data = =data)
{Temp->prior->next = temp->Next; Temp->next->prior = temp->Prior; Free(temp); returnHead; } temp= temp->Next; } printf ("the data element is not in the linked list");
returnhead;}
Find and change operations in a doubly linked list
The lookup operation of the doubly linked list is exactly the same as that of a single-linked list, which is traversed from the head node or the first node of the list, and does not explain too much here.
The operation of changing the data field of a node in a linked list is done on a lookup basis. By traversing to find the node that stores the data element, you can change its data field directly.
The complete code for this section
#include <stdio.h>#include<stdlib.h>
typedefstructLine
{structLine *Prior; intdata; structLine *Next;} Line
Line*initline (Line *head); Line*insertline (Line *head,intDataintadd); line*delline (Line *head,intdata);voidDisplay (line *head);
intMain ()
{ Line*head =NULL; Head=Initline (head); Head=insertline (Head,4,2); Display (head); Head=delline (Head,2); Display (head);
return 0;}
Line*initline (Line *head)
{Head= (line*)malloc(sizeof(line)); Head->prior =NULL; Head->next =NULL; Head->data =1; Line*list =Head; for(intI=2; i<=3; i++)
{ Line*body = (line*)malloc(sizeof(line)); Body->prior =NULL; Body->next =NULL; Body->data =i; List->next =body; Body->prior =list; List= list->Next; }
returnHead;}
Line*insertline (Line *head,intDataintadd)
{//Create a new data-domain nodeLine *temp = (line*)malloc(sizeof(line)); Temp->data =data; Temp->prior =NULL; Temp->next =NULL; //INSERT into the list header, special consideration if(Add = =1)
{Temp->next =Head; Head->prior =temp; Head=temp; }
Else
{ Line*body =Head; //Locate the previous node where you want to insert the location for(intI=1; i<add-1; i++)
{Body= body->Next; }//Judging the condition is true, indicating that the insertion position is the end of the chain if(Body->next = =NULL)
{Body->next =temp; Temp->prior =body; }
Else
{Body->next->prior =temp; Temp->next = body->Next; Body->next =temp; Temp->prior =body; } }
returnHead;}
Line*delline (Line *head,intdata)
{ Line*temp =Head; //traversing a linked list while(temp)
{//determines whether the data field in the current node is equal or not, and if it is equal, remove the node. if(Temp->data = =data)
{Temp->prior->next = temp->Next; Temp->next->prior = temp->Prior; Free(temp); returnHead; } temp= temp->Next; } printf ("the data element is not in the linked list");
returnHead;}
//function Functions of the output listvoidDisplay (line *head)
{ Line*temp =Head; while(temp)
{if(Temp->next = =NULL)
{printf ("%d\n",temp->data); }
Else
{printf ("%d->",temp->data); } temp=temp->Next; }}
Summarize
The only difference between a doubly linked list and a single linked list is that there is more than one pointer to the direct forward direction in the structure, and the other is exactly the same. If the problem requires frequent calls to the current node's forward node, then the data structure using the doubly linked list is the best solution.
Complement: The combination of a doubly linked list and a circular list
Joseph Ring problem can actually play this way: if a clockwise count, someone out, clockwise to find out the position of the next person, start the opposite direction (that is, counter-clockwise) count, someone out, counter-clockwise to find out the position of the next person, began to count clockwise. Repeat until the last dequeue.
For example, or from the start number of 3, the number of people to 2 is out:
Fig. 4 Joseph Ring
The order of the new play is:
First turn clockwise, 4 number 2, so 4 dequeue;
Clockwise to find the next person for 5, start counterclockwise turn, 3 number 2, so 3 out;
Counter-clockwise find the next person for 2, start clockwise turn, 5 number 2, so 5 dequeue;
Clockwise to find the next person for 1, start counterclockwise turn, 2 number 2, so 2 out;
Finally only 1 left, so 1 out of their own.
For the new Joseph ring problem, it is necessary to combine the circular link list and the doubly linked list, which consists of two-way circular linked list.
Interested can try coding to solve the new Joseph ring problem.
Data Structure 8: The establishment of two-way linked list (two-way cyclic link list) and C language implementation