Data Structure-one-way linked list of linear tables-1.1 drops

Source: Internet
Author: User
Tags table definition
Unidirectional linked list

Unidirectional linked listA single-chain table is a type of linked list. Its link direction is unidirectional. Access to the linked list must start from the header through sequential reading.

The data structure of a one-way linked list can be divided into two parts: data domain and pointer domain. The data domain stores data, and the pointer domain points to the address of the next storage node. It can be divided into dynamic one-way linked list and static one-way linked list. A one-way linked list can also be divided into a leading node structure and a non-leading node structure based on whether or not to take the lead node. We set the pointer to a single-chain tableHeader pointer. The first node that does not store data elements referred to by the header pointer is calledHeader Node. Nodes that store data elements becomeFirst Data Element Node.

Note: The First Data Element Node is the second node in the single-link table of the lead node, and the first node in the single-link table of the non-lead node.

Comparison between a single-chain table of a lead node and a single-chain table of a non-lead Node

The main reason for the comparison is that the deletion and insertion operations of the single-chain table of the leading node and the non-leading node are different, but the header pointer is prone to errors. The comparison content is as follows:

  1. If you select a single-chain table of the lead node and set the header pointer to head, the element is inserted before the first data element node, without changing the value of the header pointer head, the value of the pointer field pointing to the header node is changed. That is, the value of head-> next is changed. When the Temporary Variable P is equivalent to the head, the value of the pointer p-> next is changed, which is the same as the process of inserting nodes before other element nodes; similarly, when the first data element node is deleted, the Head Value of the header pointer is not changed, that is, the head-> next value is changed. When the temporary variable is equivalent to the head, the change is the value of P-> next. This is the same as deleting element nodes in other locations.
  2. If you select a single-chain table with no leading nodes and set the header pointer to head, the element is inserted before the first data element node. The value of the header pointer head is changed to the value of the S pointer. (S is equivalent to the node element to be inserted). If the element S is inserted elsewhere, the header pointer head will not change, but the value of the Temporary Variable P-> next will be changed. For example, when the first element node is deleted, the value of the header pointer head is changed to head-> next. When other element nodes are deleted, the value of the header pointer head is not changed, the value of the Temporary Variable P-> next is changed.

If you are confused, you can draw a prototype diagram. Head is the header pointer, and node S is the element node to be inserted. P refers to the n-1 node that you insert or delete in a single-chain table. (For example, if you insert data before 4th nodes, P points to 3rd nodes ).

Node definition and basic operations of a single-chain table
1/******** single-chain table definition *******/2 typedef struct Node 3 {4 datatype data; 5 struct node * next; 6} slnode; 7 8/* 9 * initialize a single-chain table 10 * Before initialization, the header pointer parameter head has no specific address value, during initialization, the 11 * Header pointer head obtains the specific address value, and the address value must be returned to 12 * Call functions. Therefore, at this time, the header pointer parameter head should be designed as the pointer type 13 * If the header pointer type is set to the pointer type at this time, then the caller will not be able to get the value of the header pointer parameter assigned to the first 14 * enable function 15 */16 17 18 void initiatelist (slnode ** head) 19 {20 * head = (slnode *) malloc (sizeof (slnode); 21 (* head)-> next = NULL; 22} 23 24/* Get the number of element nodes in the single-chain table */25 int getlistlength (slnode * head) 26 {27 slnode * P = head; 28 int size = 0; 29 While (p-> next! = NULL) 30 {31 P = p-> next; 32 size ++; 33} 34 return size; 35} 36 37/* I (0 <= I <= size) of the head of the single-chain table on the lead Node) insert a node */38 int insertlistnode (slnode * head, int I, datatype X) 39 {40 slnode * P, * q; 41 P = head; 42 Int J =-1; 43 while (p-> next! = NULL & J <i-1) 44 {45 p = p-> next; 46 J ++; 47} 48 if (J! = I-1) 49 {50 printf ("insert position is error \ n"); 51 return 0; 52} 53 q = (slnode *) malloc (sizeof (slnode )); 54 Q-> DATA = x; 55 Q-> next = p-> next; 56 p-> next = Q; 57 Return 1; 58} 59 60/* Delete the I node (0 <= I <= size-1) 61 * The data domain value of the deleted node is brought back by X, if the deletion is successful, 1 is returned. If the deletion fails, 62*0 63 */64 65 int deletelistnode (slnode * head, int I, datatype * X) 66 {67 slnode * P, * q; 68 p = head; 69 Int J =-1; 70 while (p-> next! = NULL & P-> next! = NULL & J <i-1) 71 {72 p = p-> next; 73 J ++; 74} 75 if (J! = I-1) 76 {77 printf ("error \ n"); 78 return 0; 79} 80 q = p-> next; 81 * x = Q-> data; 82 p-> next = p-> next; 83 free (Q); 84 return 1; 85} 86 87/* Data Element */88 int getlistnode (slnode * head, int I, datatype * X) 89 {90 slnode * P; 91 p = head; 92 Int J =-1; 93 while (p-> next! = NULL & J <I) 94 {95 p = p-> next; 96 J ++; 97} 98 If (J! = I) 99 {100 printf ("error \ n"); 101 return 0; 102} 103 * x = p-> data; 104 return 1; 105} 106 107/* undo a single-chain table */108 void destroylist (slnode ** head) 109 {110 slnode * P, * q; 111 P = * head; 112 while (P! = NULL) 113 {114 q = P; 115 P = p-> next; 116 free (Q); 117} 118 * head = NULL; 119}
View code single-chain table instance

Create a single-chain table, input 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 element nodes for the first time, and then delete the node of element 5.

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 typedef int DataType; 5 #include "SLNode.h" 6 int main(void) 7 { 8     SLNode *head; 9     int i = 0,temp;10     InitiateList(&head);11     for(i=0;i<10;i++)12     {13         InsertListNode(head,i,i+1);14     }    15     DeleteListNode(head,4,&temp);16     printf("It will delete node value is %d\n",temp);17     for(i=0;i<GetListLength(head);i++)18     {19        GetListNode(head,i,&temp);20        printf("%d\t",temp);21     }22     printf("\n");23     DestroyList(&head);24     return 0;25 }
View code

 

Data Structure-one-way linked list of linear tables-1.1 drops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.