[Data Structure] Basic Operations on Single List

1. Initialization

//Create an empty single-linked list LinkList InitiateLinkList( ){ LinkList head; //Head pointer head = malloc(sizeof(node)); //Build a node dynamically, which is the head node head ->next = NULL; return head;}

2. Find the table length

//Find the length of the head of the singly linked list int LentghLinklist ( LinkList head ){ Node * p = head; //p is the working pointer, initially p points to the head node int cnt = 0; //The counter is set to the initial value while(p->next != NULL) //Judge whether it is End node {p = p->next; //The pointer moves to the next node cnt ++;} return cnt;}

3. Table reading elements

//Find the i-th element node in the head of the singly linked list, and if found, return to it The pointer of the node, otherwise it returns NULLNode * GetLinklist(LinkList head,int i){ Node *p; //p is the working pointer p = head ->next; //In the beginning, p points to the head node
      int c = 1; while ((cnext; c++;} if (i == c) return p; //find the i-th node else return NULL; //i<1 or i>n, the value of i is invalid, the search fails}

4. Positioning

< /p>

//Find the first value in the table head equal to The serial number of the node of x, if there is no such node, the return result is 0int LocateLinklist( LinkList head, DataType x){ Node * p = head; //p is the working pointer p = p->next; //Initial When p points to the first node int i=0; //i represents the serial number of the node, where the initial value is set to 0
 while (p!= NULL && p->data !=x) //Access linked list {i++; p = p->next;} i f (p!= NULL) return i+1; else return 0;}


5. Insert

6. Delete

span>

Leave a Comment

Your email address will not be published.