Two types of storage structures for linear tables-chained storage
Definition: A set of arbitrary storage units to store data elements of a linear table, this set of storage units can exist in memory not occupied anywhere
We call the domain where data element information is stored, and the field where the direct successor is stored is called the pointer field. The information stored in a pointer field is called a pointer or chain. These two pieces of information comprise a data element called a storage image, called a node.
Single linked list
650) this.width=650; "title=" 11.jpg "alt=" wkiom1sq5-3haylkaab9pvw72og565.jpg "src=" http://s3.51cto.com/wyfs02/M02/ 57/14/wkiom1sq5-3haylkaab9pvw72og565.jpg "/>
The difference between head node and head pointer
Head pointer:
-A pointer to the first node in the list, or a pointer to the head node if the list has a head node.
-The head pointer has the function of marking, so the name of the common head pointer to the linked list
-The head pointer is not empty regardless of whether the linked list is empty
-The head pointer is the necessary element of the linked list
Head node:
-The head node is established for the unification and convenience of the operation, and the data field is generally meaningless before the node of the first element is placed.
-With a head node, the operation of inserting a node and deleting the first node before the first element node is unified with other nodes.
-The head node is not a necessary element of a linked list
Single-linked list legend
650) this.width=650; "title=" 11.jpg "alt=" wkiom1sq6bexepeqaaa1zjveq5s145.jpg "src=" http://s3.51cto.com/wyfs02/M02/ 57/14/wkiom1sq6bexepeqaaa1zjveq5s145.jpg "/>
Empty linked list
650) this.width=650; "title=" 11.jpg "alt=" wkiol1sq6ociwffaaaajgiomrvm193.jpg "src=" http://s3.51cto.com/wyfs02/M02/ 57/12/wkiol1sq6ociwffaaaajgiomrvm193.jpg "/>
Using structure pointers to describe the single-linked table storage structure
typedef struct NODE
{
Elemtype data;//Data domain
struct node* next;//pointer field
}node;
typedef struct NODE* linklist;
Single-linked list reading
Ideas:
① declares a node p points to the first node of the list, initialization J starts at 1.
② when on the J<i, it traverses the list, so that P's pointer moves backwards, pointing to the next node, j+1;
③ If the end of the list P is empty, then the first element does not exist;
④ Otherwise the lookup succeeds, return node P data
Sample code
Status Getelem (linklist l,int i,elemtype *e)
{
Int J;
Linklist p;
p = l->next;
j = 1;
while (P && j<1)
{
p = p->next;
++j;
}
if (!p | | j>i)
{
return ERROR;
}
*e = p->data;
return OK;
}
Time complexity O (n)
Single-linked list insertion
For example, insert S junction between AI and Ai+1.
650) this.width=650; "Width=" 520 "height=" 289 "title=" 11.jpg "style=" width:346px;height:163px; "src="/HTTP/ S3.51cto.com/wyfs02/m02/57/12/wkiol1sq6srtgictaabsliqasem521.jpg "alt=" Wkiol1sq6srtgictaabsliqasem521.jpg "/>
Ideas:
-Declares a node p to point to the table header node, initializing J starting from 1;
-j<1, just traverse the table and let p move backwards, pointing to the next node (j + +).
-If the bottom of the list P is empty, then the I position to be inserted does not exist
-If the lookup succeeds, generate an S-node.
-assigns the data element E to s->data;
-Single linked list insert just two standard statements
-Return success
Code:
Status Listinsert (linklist *l,int i,elemtype e)
{
Int J;
Linklist p,s;
p = +l;
j = 1;
while (P && j<i)//Traversal of element I, starting from 1 to I end
{
p = p->next;
j + +;
}
if (!p | | j>1)//If to the end, return prompt
{
return ERROR;
}
S= (linklist) malloc (sizeof (Node));
S->data =e;//Value Assignment
The following two sentences can not be written anti-oh
S->next = p->next;
P->next = s;
return OK;
}
Single-linked list delete operation
Delete the A2 in the single-linked list a1,a2,a3
650) this.width=650; "Width=" 518 "height=" 242 "title=" 11.jpg "style=" width:342px;height:143px; "src="/HTTP/ S3.51cto.com/wyfs02/m00/57/13/wkiol1sq75iic-ueaabgm_rdfug655.jpg "alt=" Wkiol1sq75iic-ueaabgm_rdfug655.jpg "/>
Ideas:
-the declaration node p points to the first node of the list, initializing the j=1;
-When on j<1, iterate through the linked list and let the P pointer move back, pointing to the next node continuously.
-If the tail of the list is empty, then the I position to delete does not exist
-Find success, assign node P->next to Q
-Single Linked list Delete standard statement p->next = Q->next
-Assign the data in the Q node to E and return
-Release Q node
Code:
Status Listinsert (linklist *l,int i,elemtype e)
{
Int J;
Linklist p,q;
p = +l;
j = 1;
while (P->next && j<i)//Traversal of element I, starting from 1 to I end
{
p = p->next;
++j;
}
if (! ( P->next) | | J>1)//If you go to the end, return a hint
{
return ERROR;
}
Q = p->next;
P->next = q->next;
*e = q->data;
Free (q);
return OK;
}
The more frequent the insert and delete operations, the more efficient the single-linked list becomes
Data structures and algorithms-Learning Note 5