Copyright NOTICE: This article is the original article of the blogger, without the permission of the blogger may not be reproduced.
Directory (?) [+]
Today will tell you about the linked list of learning experience. Learning data structure, no doubt linked list must learn, the back of the stack, queues, trees, graphs are based on the linked list; many types of linked list, there are single linked list, double linked list, circular chain table, non-circular chain list; here, we take the non-circular single linked list as an example to tell the creation, length, sorting, insertion and sorting of the linked list. 1. What is a linked list
List of my understanding to include the following characteristics: (1). Discrete distribution by N nodes (2). Each node is connected by a pointer (3) Each node consists of a predecessor node and a rear-drive node (4). There is no precursor node in the first node, and the tail node has no rear drive node;
To meet the above 4, we are called the linked list, and since the list is made up of many nodes, then what constitutes the node. The node consists of two parts, one is the data field, which is used to store valid data; the second is the pointer field, which is used to point to the next node; The following uses the C language to construct the linked list data structure, first should construct the node, then all nodes connect together, form the chain list;
(1) The construction of the node
[CPP] view plain copy typedef struct NODE {int data;//data field, for storing data fields; struct node *pnext;//define a struct Body pointer pointing to the next node with the same data type as the current node}node,*pnode; node is equivalent to struct node; Pnode is equivalent to struct Node *; here uppercase is used to differentiate with variables, and can make it easy to be a data type typedef simply takes the name of the data type, that is, the TypeDef data type alias; we know that struct Node is the data we define. Type
(2) The creation of the linked list
Before we create a linked list, we need to know the technical terms:
First node: the node that holds the first valid data;
Tail Node: The node that holds the last valid data;
Head node: The data type of the head node is the same as the data type of the first node, and the head node is the node in front of the first node and does not hold valid data; The head node exists only to facilitate the operation of the linked list.
Head pointer: A pointer to a head node;
Tail pointer: pointer to tail node;
First, we should create a head node and point to it with the head pointer, in C language: Apply a piece of memory to the computer with malloc, and define a pointer to the same data type as the header node (be sure to determine if the application memory is successful);
Then, to know the length of the list to create, use a loop to create one node at a time and connect each node together;
If we were to insert the node p after the head node Phead:
(1) The pointer field of the head node points to p node, i.e. phead->pnext=p;
(2) Point the Pointer field of P node to NULL, i.e. p->pnext=null;
Is that OK? Think about it and we can see when we want to insert more than one node, the head node always points to the last data added, and the previous node is unable to find it through the head pointer at this time; we define a tail pointer ptail that always points to the end of the list, adding nodes only after ptail at a time.
Pseudo algorithm:
(1) Defining a tail pointer ptail and initializing it to point to the head node, i.e. Ptail=phead;
(2) Add nodes after Ptail and modify the pointer:
ptail->pnext=p;
p->pnext=null;
Ptail=p; Make Ptail point to the last element of a linked list
[CPP] View Plain copy pnode create_list (void) { int len; //Storage list Length int i; //cyclic variable int val;//is used to temporarily store the value of the node entered by the user pnode List; pnode phead= (pnode) malloc (sizeof (node));//Assign a head node if (null==phead) { printf ("Memory allocation failure"); exit ( -1); } else { PNODE pTail=pHead; pHead->pNext=NULL; printf ("please input the length of list: "); //need a pointer to always point to the end of the list scanf ("%d", &len); for (i=0;i<len;i++) { pnode p= (pnode) malloc (sizeof (NODE)); if (null==p) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRINTF (" Memory allocation failure "); exit ( -1); } else { printf ("Please input the value of list: "); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &val); &