Linked list is a very important basic part of data structure, below I through simple story to link list of content to explain, but also summed up their learning content:
Story:
One day, Lele, Fengfeng, nerd, star, Collar, Xiao Wei 6 children lead 8 children together to play on the mountain. After the play, the world began to rain!! So the 14 children hurried back, unfortunately, Yamaguchi Mountain flash flood outbreak. If you want to be in the past, 14 children need to be linked together, a single child crossing the river will be swept away by the torrent (because the experiment proved this, and Wei in the test process was washed away by the flood).
We regard each child as a node .
typedef struct LNODE{INT data; Children data struct Lnode *next; A pointer to the next child};
Create a single linked list:
Then to get past the flood, 14 children need to build a long team. As you can imagine, there are two ways to build a long team:
First we need to empty out a field to build our long team (said to build an empty list)
Code
void Initlist (linklist*&l) {L = (linklist *) malloc (sizeof (linklist)); L->next = NULL;}
The First kind: Lele stands in the first, the star stands in front of Lele, the nerd stands in front of the star ... In order, so that Le will eventually stand at the end of the team (this is the head interpolation to create a single-linked list ).
1.2.3 ..... Fengfeng, Nerd, star, Lele.
Code
void Createlisth (linklist *&l, int a[], int n) //head interpolation to create a single-linked list {linklist *s; S is the child to be inserted int i; L = (linklist *) malloc (sizeof (linklist)); Application space, L->next = NULL; At first it was empty because there was no line up. for (i = 0; i < n; i++) //We will then insert the child in front, and let the child's hand hold the children's clothes standing behind him {s->data = A[i];s->next = l->next; L->next = S;}}
The second kind: Lele stands in the first, the star stands in the back of Lele, the nerd stands behind the star, Fengfeng stands behind the nerd, ... The other person in turn in the back row (this is the tail interpolation method to create a single linked list ).
Lele, star, nerd, Fengfeng ...
Code:
void Createlistr (linklist *&l, int a[], int n) {int i; Linklist *s, *r; L = (linklist *) malloc (sizeof (linklist)); Application Space R = l;for (i = 0; i < n; i++) //S is the child to be inserted, R is just a temporary marker, in order to know who is now in the final position {S->data = A[i];r->next = S; Insert S children into the back of R, R = S; After inserting, then s in the final position, so that it becomes a marker. Because we need to know who is on the last side, the next insert operation}r->next = l->next; After the team is established, the last team tail node is NULL;}
So now the team has been established, can cross the river, the children are very happy (in fact, not happy).
This time Xiao Wei unexpectedly came back, the children are very happy he or alive, elected him as the captain. Because the small Wegang back, the situation is not very understanding of the team, he wants to know how many people, so he from the head to the end of the team to check the number:
(Number of data nodes in a single-linked list)
Code
int Listlength (linklist *l) {int ans = 0; linklist *p = L; Wei Seniors found the team head while (p->next! = NULL) //until the tail, see a little child on the ans++;{ans++;p = P->next;} return ans;}
But the tragedy is that, because of the limited IQ, can only count to 10, it will not count. So he thought of a way to let the children of the team say their names, information:
(Information stored by the output node):
Code
void Coutlist (linklist *l) {linklist *p = l->next; Start from the active node while (P! = NULL) {printf ("%d", p->data); The children shout out their message P = p->next; Replace a Child}}
As the small Wei can only count to 10, causing the stars in the team to ridicule, and gave him a nickname: Small white. Standing in the head of the team's small Wei is angry, angry Wei suddenly know how to count 10 after the number. So he looked at the location of the children who laughed at him and gave him a nickname for revenge:
To modify the data information for a node:
Code
BOOL Changeinfo (linklist *&l, int i, int &e) //small Vinča to the position of the stars, think well of the nickname {int j = 0; linklist *p = L;while (J < i && p! = NULL) //If it is not in the data range, it is explained that Xiao Wei's math is really very vegetable {j + +;p = P->next;} if (p = = NULL) return false;else{p->data = e; If I find the star in the position of I, Wei will give the star his good nickname to return true;}}
Because of the behavior of Wei, so that the lele in a position is very angry. So Wei looked at Lele's position, and exercised the captain's right to kick out of the team (because Wei know Lele future will be an IT great God, to let Lele hang off first).
To delete a node:
BOOL Listdelete (linklist *&l, int i) //Find Lele's position {int j = 0; linklist *p = L, *q;while (J < i-1 && P! = NULL) {j + +;p = P->next;} if (p = = NULL) //If the error is found, there is no I node, stating that Xiao Wei's math is the physical education teacher, return false;else{q = p->next; If you find Lele, put Lele a feel kicked out of the team, let Lele in front of the children's hand pull lele behind the children's clothes if (q = = NULL) {return false;} P->next = q->next; Free (q); return true;}}
Assorted things finally finished, so small Veye back. (because he didn't want to be washed away alone.)
To insert a data element:
Code
BOOL Listinsert (linklist *&l, int i, int e) //Wei Insert team at a location {int j = 0; linklist *p = L, *s;while (J < i-1 && P! = NULL) {j + +;p = P->next;} if (p = = NULL) return false;else{s = (linklist *) malloc (sizeof (linklist)); s->data = E;s->next = P->next;p->nex t = S;return true;}}
Then now began to cross the river, because the river suddenly soared, Wei seniors unexpectedly was washed away. The little buddies need to get tighter, so they come back and rebuild the team. And the way to catch clothes changed a bit: Let the previous child's hand grasp after a child's clothes, after a child's hand grabbed him in front of the children's clothes. i.e. (double linked list)
There are two ways to build a doubly linked list at this point, similar to creating a single-linked list, with only one precursor node to add:
Code
typedef struct LNODE{INT data;struct lnode *prior; Precursor node struct lnode *next; Successor node}linklist;
The first method of establishing the head interpolation (similar to the single-link head interpolation method):
Code
void Createlist_f (linklist *&l, int a[], int n) {linklist *s;int i; L = (linklist*) malloc (sizeof (linklist)); L->prior = L->next = null;for (i = 0; i < n; i++) {s = (linklist *) malloc (sizeof (linklist)); s->data = A[I];S-&G T;next = L->next;if (l->next! = NULL) {L->next->prior = s;} L->next = S;s->prior = L;}}
The tail interpolation method establishes the doubly linked list:
Code
void Creatlist_r (linklist *&l, int a[], int n) {int i; Linklist *s, *r;r = l;for (i = 0; i < n; i++) {s = (linklist*) malloc (sizeof (linklist)); s->data = A[i];r->next = S; S->prior = r;} R->next = L->next;}
After a lot of bumpy, finally arrived at the other side of the river, so the small partners in hand in a circle, cheerful cry up.
Circular link list:
The loop list simply points to the beginning of the linked list with the next trailing node of the linked list;
Let me summarize the following code:
Single Linked list:
typedef struct LNODE//Data node {int data;struct lnode *next;} Linklist;void initlist (linklist *&l)//Create empty single-linked list {L = (linklist *) malloc (sizeof (linklist)); L->next = NULL;} void Createlisth (linklist *&l, int a[], int n)//head interpolation to create a single-linked list {linklist *s; int i; L = (linklist *) malloc (sizeof (linklist)); L is the head of the linked list l->next = NULL; for (i = 0; i < n; i++)//inserts data into the chain header (L) after {s->data = A[i];s->next = l->next; That is: s pointing to L, l pointing to S;l->next = s;}} void Createlistr (linklist *&l, int a[], int n)//tail interpolation to create a single-linked list {int i; Linklist *s, *r; L = (linklist *) malloc (sizeof (linklist)); Application space. ; next = s; R is the tail of the team, insert s into the back of R, R = s; After inserting, then s in the final position, so that it becomes a marker. Now S is the new team tail}r->next = l->next; After the team is established, the last team tail node is Null;}</pre><pre code_snippet_id= "1630697" snippet_file_name= "blog_20160331_15_6131477" name= "code" class= "CPP" >int listlength (LinkList *L) Returns the length of the single-linked list {int ans = 0; linklist *p = L; (Note that linklist * p = L) while (p->next! = NULL)//If the next node is not empty, there must be data stored, so ans++;{ans++;p = P->next;} return ans;} void Coutlist (linklist *l)//output information for each node {linklist *p = l->next; Start from scratch (Note that linklist *p = l->next) while (P! = NULL)//If the current node is not empty, the output information {printf ("%d", P->data);p = P->next;}} BOOL Getelem (linklist *&l, int i, int &e)//Modify data information at a location {int j = 0; linklist *p = L;while (J < i && p! = NULL)//If not traversed to i-1 and the list does not end {j + +; Then find p = p->next;} if (p = = NULL)//If the end is the end of the list, then I am out of range return False;else Otherwise, modify the information {P->data = E;return trUE;}} BOOL Listdelete (linklist *&l, int i)//delete a node {int j = 0; linklist *p = L, *q;while (J < i-1 && P! = NULL) {j + +;p = P->next;} if (p = = NULL) return false;else//If the node is found, {q = p->next; So the PostScript node of the current node points to the back of it, which is equivalent to isolating the node from the IF (q = = NULL) {return false;} P->next = Q->next;free (q); return true;}} BOOL Listinsert (linklist *&l, int i, int e)//Insert Node {int j = 0; linklist *p = L, *s;while (J < i-1 && P! = NULL) {j + +;p = P->next;} if (p = = NULL) return false;else//If location is found, first the S node to be inserted points to the successor of the current node, and the successor of the current node points to S. {s = (linklist *) malloc (sizeof (linklist)); s->data = E;s->next = P->next;p->next = S;return true;}}
double linked list:
typedef struct LNODE //Doubly linked list node {int data;struct lnode *prior;struct lnode *next;} Linklist;void Createlist_f (linklist *&l, int a[], int n) //head interpolation to create a doubly linked list {linklist *s;int i; L = (linklist*) malloc (sizeof (linklist)); L->prior = L->next = NULL; First the head node of next and Prior is empty for (i = 0; i < n; i++) {s = (linklist *) malloc (sizeof (linklist)); s->data = A[i];s->next = L->next;if (L->next! = NULL) //If Next of the head node is not empty, then the prior of the next node of L must point to S{l->next->prior = s;} L->next = s; The next point of L points to the S,s's predecessor node, pointing to L. S->prior = L;}} void Creatlist_r (linklist *&l, int a[], int n) tail interpolation to establish a doubly linked list {int i; Linklist *s, *r;r = l;for (i = 0; i < n; i++) {s = (linklist*) malloc (sizeof (linklist)); Just let the new node 's predecessor node point at the end of the team, regardless of the head L, because it is the tail interpolation method. S->data = A[i];r->next = S;s->prior = r;} R->next = L->next;}
Circular linked list is not written, because only need to let the tail node and the head junction on the line, but pay attention to the double-linked list cycle and single-linked list cycle is a certain difference. But the essence is the same.
Hopefully this article will help you to better understand or review the list operation.
If wrong, please leave a message.
Data structure: A detailed list of links