# The single linked list (c) will be supplemented when the strength is enough

catalogue

1: Node creation

3: Find the number of elements in the single linked list

4: Insertion and deletion of single linked list and release of all nodes

insert

delete

Release all nodes

5: Implementation of single linked list (each time a new node is created, the next field of the node must be set to empty)

Tail interpolation

# 1: Node creation

Create nodes through structures in c

Let's take the single linked list as an example and look at the following code. This is the structure type that creates a node

```typedef struct node {//Structure creation
int date;//Data domain
struct Node* next;//Pointer field
}Node,*Link;//Alias. One is the structure type and the other is the pointer to the structure type```

We create a node through malloc to reference #include < malloc. H >

The return value of malloc function is void * universal pointer type. This pointer type cannot be operated and can be forcibly converted to any type

Since l1 is of Link type, it is converted to Link type

```	Link l1=(Link) malloc(sizeof(Node));//Create a node. The return value of malloc is void * and can be forcibly converted to any type
```

# 2: Traversal of linked list

It is easy to traverse the linked list by moving the pointer, but not p + +, because the linked list is logically continuous and physically discontinuous. Look at the following code. There is a header node here, and the next node of the header node stores data

```void Display(Link l1) {//Traversal of single linked list
while (p) {//Determine whether p is empty
printf("%d ", p->date);
p = p->next;
}

}```

# 3: Find the number of elements in the single linked list

This is still traversal

```int Num(Link l1) {//Calculate from the initial node. If you calculate from the initial node, count=1
int count = 0;
while (l1) {//Judge whether l1 is empty
l1 = l1->next;
count++;
}
return count;
}```

# 4: Insertion and deletion of single linked list and release of all nodes

### insert

The precursor is easier to use than the successor. Ouch

The pointer points to the previous node of the inserted node, that is, the precursor ```int  insertNode(Link l1, int n, int x) {//l1 is the head pointer of the linked list, n is the insertion position, and x is the inserted data
int count = 0;//Record where the pointer went
while (p != NULL && count < n - 1) {//n is the insertion position. We need to move the pointer to the precursor position because the precursor ratio
//Subsequent easy to use [0-n-1] is n-1 number
p = p->next;
count++;
}
if (p == NULL) {//The linked list is not long enough
return 0;
}
else {
node->date = x;
node->next = p->next;//Insertion algorithm
p->next = node;
}
return 1;
}```

### delete

Two pointers need to be scanned one after the other until the previous pointer is scanned to the node to be deleted. Before scanning, judge whether the linked list is not empty ```Link  my_delete(Link head, int n) {
return 0;
}
while (p) {//Prevention of cross-border
if (p->date == n) {//If equal, delete
q->next = p->next;
free(p);
return 1;
}
else {//The two pointers move forward
q = p;
p = p->next;
}
}
return 0;
}```

### Release all nodes

```void my_free(Link head) {
while (head->next) {//Release if not empty
free(p);
}
}```

# 5: Implementation of single linked list (each time a new node is created, the next field of the node must be set to empty)

Suppose we have an array with n elements. We need to put the elements in a single linked list

The order of inserting elements by head interpolation is reversed

```#include<stdio.h>
#include<malloc.h>
typedef struct node {
int date;
struct node* next;
Link newList(int *arr, int n) {
for (int i = 0; i < n; i++) {
node->date = *arr++;//Put the elements in the array into the data field of the node
node->next = head->next;//Set the pointer field of the node to null
}
}
int main() {
int arr = { 1,2,3,4,5 };
while (p->next) {
p = p->next;
printf("%d ", p->date);

}

}``` ### Tail interpolation

```Link newList2(int *arr, int n) {
for (int i = 0; i < n; i++) {
node->date = *arr++;
rest->next = node;//Point the tail node pointer field to the new node
rest = node;//Point the tail node to the new node
}
rest->next = NULL;//Set the pointer field of the tail node to null, otherwise a wild pointer will appear, pointing to the unknown field 