I have almost graduated recently, and I have reviewed some of my knowledge. this includes the forgotten linked list knowledge, which is a very important knowledge point in C language. at the same time, I found that many people will forget this knowledge. Therefore, this article helps you to recall the linked list and help students who are new to C. I used the Q & A method to review the knowledge and hope to accept it!
Tip: This article referencesLi Fengxia (beili)'s C Language Programming Tutorial and courseware andC program design by Tan haoqiang (Tsinghua).
I. Basic concepts of linked lists
1. What is a linked list?
A linked list is a common data structure for dynamic storage and distribution.
2. Why is the linked list structure?
(1 ). when using arrays to store data in C language, you must first define a Fixed Array length to determine the number of elements. if the data exceeds its capacity, array overflow will occur. To prevent this overflow, a large array is often defined, which leads to a waste of resource space. if the program uses the dynamic array method to copy the increasing data, the method is feasible but the efficiency is too low;
(2 ). if you want to delete a data or insert a data in an array, you need to move the data after the delete or insert point array in sequence. This will also lead to low program efficiency.
3. At this time, the structure of the linked list to dynamically store data comes into being. Do you see some simple differences between arrays and linked lists? What is the basic unit of the linked list?
Nodes are the basic storage units of the linked list. All elements in the linked list are stored in a node with the same data structure. A node corresponds to a set of data elements. Each node uses a continuous storage space in the memory (a node can be composed of multiple data domains), and each node uses a non-sequential storage space, nodes are linked by pointers. A node consists of a data domain and a pointer domain/chain. the common definitions are as follows:
Struct node {dadatype data; // data field struct node * next; // pointer field: pointing to node pointer };
4. After knowing the basic storage unit of the linked list, what is the basic component of the linked list?
A linked list is generally composed of three parts:
(1). header pointer: a pointer to the head node of the linked list. The header pointer is a symbol of the linked list. Generally, head is used to define the head pointer;
(2). header node: The first node of the linked list, which generally does not store data information. There can be no header node in the Linked List (described later), which is used to facilitate the introduction of nodes.
(3). Data Node: the node that actually saves data information is as follows:
5. As mentioned above, there may be no header node in the linked list, what are the common forms of the linked list?
Common forms include: one-way linked list with a header node, one-way linked list without a header node, one-way circulating table with a header, one-way circulating table without a header. the difference between a header and a non-header is whether a header node exists. The insert/delete operation has different judgments; the difference between a one-way linked list and a one-way cyclic linked list is whether the pointer to the last data node is NULL or to the header node. A two-way linked list refers to the linked list at the forward position and the last position respectively.
6. What are the common operations in the linked list?
Common Operations of a linked list include: creating a linked list, traversing a table, calculating the length of a linked list, inserting data, and deleting a node.
2. Basic operations on the linked list 1. Create a linked list
Before creating a linked list, define a structure type that contains the data and pointer fields, create a header pointer head pointing to the header node, and dynamically apply for memory as the header node through the malloc function. the header file of void * malloc (int size) is "stdlib. h ". dynamically allocate Length size byte storage zone.
// Define the structure type typedef struct node {char name [20]; // data field struct node * next; // pointer field} NODE; NODE * head, * p; // description pointer // create an empty linked list (only the header NODE) p = (NODE *) malloc (sizeof (NODE); p-> next = NULL; head = p; // insert a data NODE p = (NODE *) malloc (sizeof (NODE); gets (p-> name ); // enter the name p-> next = head-> next; // p points to the next node = head points to the next node and lead-> next = p; // After the p node is inserted into the head of the header Node
The execution process of the above Code is shown in:
If you want to use a function to create a linked list, the Code is as follows:
// Create the void create (NODE * head, int n) {NODE * p; for (; n> 0; n --) {p = (NODE *) malloc (sizeof (NODE); gets (p-> name); p-> next = head-> next; head-> next = p ;}}
However, note: When this method is used, a new node is always inserted after the head, which leads to the insertion order storing information of the n nodes in the reverse order of the input order. if you want to insert data sequentially, you only need to point the head node to the first insertion node p, the first to the second, and the last node to the NULL in turn. in the Joseph loop, I will talk about it.
2. traversal table
You can use the output function to find a node in a traversal chain table starting from the first node of the chain table. If you want to add some traversal conditions, you can add them to the function, the output function below outputs the Student name in sequence.
// Traverse the output result void output (NODE * head) {NODE * p; p = head-> next; // contains the header NODE while (p! = NULL) {puts (p-> name); p = p-> next ;}}
If you want to calculate the length of the linked list, if you want to traverse the table from the first node when the table header is included, the length of one node cannot be found until the end of the linked list. if the linked list is empty, the header node head-> next = null. the returned value is 0.
// Calculate the length of the linked list int count (NODE * head) {int number = 0; NODE * p; p = head-> next; while (p! = NULL) {number ++; p = p-> next;} return number ;}
The custom main function calls its function. The program test result is shown in. Whether the reverse sequence is clear at a glance.
3. insert data
The algorithm for inserting a new node after node I in the linked list is as follows:
(1) locate the I-th node. Point q to the I-th node, and point p to the node to be inserted.
(2) pointer after link: p-> next = q-> next.
(3). pointer before link: q-> next = p.
As shown in:
As shown in the Code, when using the insert function to insert a new node, there may be two special cases: one is to insert a new node to the empty table, and the other is to insert a new node to the end of the last element of the linked list.
// Insert new NODE head pointer p insert pointer I position void insert (NODE * head, NODE * p, int I) {NODE * q; int n = 0; q = head; // find the position of node I in step 1 while (n <I & q-> next! = NULL) {q = q-> next; n ++;} // the pointer p-> next = q-> next after the second link; // link the pointer q-> next = p;} in step 3 ;}
4. delete data
You can delete any data node in the linked list. The algorithm for deleting the I node in the linked list is as follows:
(1). Locate the I-1 node position. pointer q points to the I-1 node, pointer p points to the deleted node.
(2). Link extraction: q-> next = p-> next.
(3). Release node p: free (p ).
Void free (void * p) releases the memory space pointed to by p. The header file is "stdlib. h", as shown in:
As shown in the Code, after deleting node p, you need to assign a value of p to the next new node pointed by q to continue the delete operation.
// Delete the I-th NODE void delete_node (NODE * head, int I) {NODE * q, * p; int n = 0; q = head; // The first step to find the I-1 node position q pointer pointing to while (n <I-1 & q-> next! = NULL) {q = q-> next; n ++;} if (q-> next! = NULL) {// In the second step, q points to the next node p = q-> next; q-> next = p-> next; // step 3 release the node free (p );}}
I hope that the reader will think about a problem. When deleting a knot, what will happen if the link pointer does not release the node free after the link extraction operation? If you have learned C ++ or Jave, can you recall its memory management and leakage knowledge?
Iii. Classic linked list problem-Joseph's Cycle Problem
Can you recall some simple knowledge of linked list? Next I want to see how the linked list can be applied to instances through the classic topic "Joseph loop problem" in the linked list knowledge.
Question: There are N Children in a circle and number them in sequence (starting from 1). The teacher specifies to report the number of children starting from the nth day. When the number of children is reported to the nth day, the next child continues to report data starting from 1 and columns them in sequence. ask for the order of the child or the number of the last child.
Input: Enter n, m, and s ).
Output: the Children's column sequence or the last child number.
Analysis: As shown in, when the input n = 5, m = 2, s = 3, it indicates that there are a total of five children in a circle, m = 2 indicates the number of data records for the second child, 1 for the second child, 2 for the third child, and 3 (s = 3) for the fourth child. the column order is 4-2-1-3-5.
To complete this program, you need:
(1 ). create a one-way circular linked list. note that in this case, table creation is performed in sequence. Insert a new node after the head as described above to create a table in reverse order. in this case, you need to insert head> a1> a2in sequence. Finally, Let q> next = head to build a circular linked list. (No header node in the Code)
(2). Find the node that starts to report the number through a loop, point p to the node that starts to report the number, and point q to the previous node, because when deleting p, you need to extract the chain through the previous node q.
(3) The number of deleted nodes is reported in a loop, and p = p-> next exits the loop. At this time, only the last node is left.
# Include <stdio. h> # include <stdlib. h> // define the structure typedef struct node {int no; struct node * next;} NODE; int main () {int I, j, k; int n, m, s; // n children start from m to report s columns of NODE * head, * p, * q; // header node p insert node q insert previous node printf ("Enter the number \ n"); scanf ("% d", & n, & m, & s); // Insert the head of a one-way circular linked list without a header in the sequence of table creation; for (I = 1; I <= n; I ++) // I storage serial number {p = (NODE *) malloc (sizeof (NODE); p-> no = I; if (head = NULL) head = p; // The first node is saved to the headelse q-> next = p; // q link the newly inserted node pq = p; // The newly inserted node constitutes the end of the chain} Q-> next = head; // The Link head at the end of the chain forms a loop. // find the output position. m p indicates the Start Node. q indicates the first node. q = head; p = head; for (k = 1; k <m; k ++) // If m = 1, that is, the first position {p = p-> next ;} while (q-> next! = P) // find the previous node {q = q-> next;} // Delete the node and output printf ("output delete node order: \ n "); while (p-> next! = P) {// find the location of the node to be deleted for (j = 1; j <s; j ++) {q = p; p = p-> next ;} // output the node and delete printf ("% d", p-> no); q-> next = p-> next; free (p ); p = q-> next;} printf ("\ n last remaining node: % d \ n", p-> no); system ("PAUSE"); return 0 ;}
The test cases and output results are as follows:
(1). Input n = 5 m = 2 s = 3
(2) Input n = 35 m = 5 s = 3
If you are a beginner in C language, I hope the article will give you some knowledge about linked lists;
If you are a postgraduate or looking for a job, I hope you will be helpful in the interview questions or postgraduate exams;
If you have a deep understanding of the linked list, I hope you can give me a smile when you read this article;
Finally, I hope this article will be helpful to you. If there are errors or deficiencies in this article, please try again! At the same time, I would like to thank my alma mater BIT and teacher for the four-year period of time. There is still a lot of knowledge to learn. This article is just a summary of my linked list knowledge and online notes. Please respect the fruits of my work!
(By: Eastmount original CSDNHttp://blog.csdn.net/eastmount/)