Single-linked list overview
The advantage of the sequential table of a linear table is that any element in the table is randomly accessed, but its disadvantage is also obvious: the need to move a large number of elements when inserting and deleting data elements into a sequential table in a basic operation. Thus, another chain-type storage structure that produces a linear table is a single-linked list. It has no weaknesses in the sequential table, but it also loses the advantage of the sequential table.
The chain storage structure of linear tables is characterized by the use of an arbitrary set of data elements of a linear table of storage units (this set of storage units can be continuous or discontinuous). Therefore, in order to represent the logical relationship between each data element AI and its direct successor data element ai+1, for data element AI, in addition to storing its own information, it is necessary to store a message indicating its direct successor (i.e. direct successor to the village mutual location). These two pieces of information make up the storage image of the data element AI, called the node. It consists of two domains where the domain where data element information is stored is called the data domain, and the domain where the direct successor storage is stored is called the pointer field. The information stored by the pointer field is called a pointer or chain. The chain of N nodes forms a linked list, which is the chain storage structure of the linear table. And because each node in this list contains only one pointer field, it is also called a linear list or a single linked list.
The following example
Zhao qian ,sun, Li, zhou,wu zheng Span style= "color:black; Font-family: ' Times New Roman '; Font-size:16pt "", wang Span style= "color:black; Font-family: Song body; Font-size:16pt ">)
Linear linked List
head pointer H
Store address data field pointer field
1 li
7 Qian
Sun 1
+ Wang NULL
wu Notoginseng
7 Zhao
Panax notoginseng Zheng
2 Zhou
access to the entire list must start from the beginning, and the head pointer indicates where the first node in the list (that is, the storage image of the first Data element) is stored. And since the last data element has no direct successor, the pointer to the last node in the linear list is "NULL".
When a linear table is represented by a linear list, the logical relationship between the data elements is indicated by the pointers in the nodes. In other words, the pointer is an image of the logical relationship between the data elements, and the logically adjacent two data elements are stored in a physical location that does not require immediate proximity, and this storage structure is a non-sequential storage image or chain image.
Usually we draw the list as a sequence of nodes connected by arrows, and the arrows between the nodes represent the pointers in the chain field. So the logical state of the linear list for the example above is:
The storage structure of a single-linked list is:
<span style= "FONT-SIZE:18PX;" >typedef struct lnode//redefine the data field of the node defined by the structure type Lnode{elemtype date;//the pointer field of the node that the struct Lnode *next;//defines}lnode,*linklist// Defines a variable of type Lnode linklist</span>
Sometimes we have a node before the first node in a single-linked list, called the head node. So the single-linked list of leading nodes is represented as:
A single linked list of images inserted and deleted is:
basic operation of a single linked list:
0 preparation prior to basic operation
<span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace std; #include <malloc.h> #include <stdlib.h> # Define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW-2 typedef int Elemtype;
typedef int Status; typedef struct LNODE//Redefine the data field of the node defined by the structure type Lnode{elemtype data;//struct Lnode *next;//the pointer field of the defined node}lnode,*linklist;// Defines a variable of type Lnode linklist</span>
1 initializing the list of links
<span style= "FONT-SIZE:18PX;" >//1 initializes the list of status Initlist (Linklist &l) {l= (linklist) malloc (sizeof (Lnode)); L) {return (OVERFLOW); } l->next=null; return OK;} </span>
2 Destruction of linked list
<span style= "FONT-SIZE:18PX;" >//2 destroy linked list status destorylist (Linklist &l) {linklist q; while (L) {q=l->next; Free (L); l=q;} return OK;} </span>
3 emptying the list
<span style= "FONT-SIZE:18PX;" >//3 Empty list status clearlist (Linklist &l) {linklist p,q;p=l->next;while (p) {Q=p->next;free (p); p=q;} L->next=null;return OK; } </span>
4 determine if the linked list is empty
<span style= "FONT-SIZE:18PX;" >//4 Determine if the list is empty status Listempty (linklist L) {if (L->next) {return FALSE;} Else{return TRUE;}} </span>
5 Returns the length of the linked list
<span style= "FONT-SIZE:18PX;" >status listlength (linklist L) {int i=0; Linklist P;p=l->next;while (p) {i++;p =p->next;} return i;} </span>
6 returns the value of the first data element of the linear table
<span style= "FONT-SIZE:18PX;" >//6 returns the value of the first data element of the linear table, status getelem_l (linklist l,int i,elemtype &e)//l The head pointer of the single-linked list for the lead node { linklist p=l->next; int j=1;//is initialized, p points to the first node, J is the indicator while (p&&j<i)//is searched clockwise, until p points to the I element or P is empty {p=p->next;++j;} if (!p| | J>i) { return error;//the first element does not exist } e=p->data;//take the first element I return OK;} </span>
7 Inserting data elements into a linked list
<span style= "FONT-SIZE:18PX;" >//7 inserting data elements into the list status Listinsert (linklist &l,int i,elemtype e) { linklist p=l; int j=0; while (p&&j<i-1) { p=p->next; ++j; } if (!p| | J>i-1) { return ERROR; } Linklist s= (linklist) malloc (sizeof (Lnode));//Generate new node s->data=e; s->next=p->next;//inserting L in p->next=s; return OK;} </span>
8 deleting elements from a linked list
<span style= "FONT-SIZE:18PX;" >//8 Delete the elements in the linked list status listdelete_l (linklist &l,int i,elemtype &e) { linklist p=l; int j=0; while (p->next&&j<i-1) { p=p->next; ++j; } if (! ( P->next) | | J>i-1) { return ERROR; } Delete location unreasonable linklist q=p->next; p->next=q->next;//Delete and release the node e=q->data; Free (q); return OK;} </span>
Single linked list of DS