# Detailed explanation of single linked list of data structure

Keywords: C data structure linked list

# Linked list of data structure

----------------------------------------------------------Daily chart--------------------------------------------------

# preface

In the last article, we analyzed and explained the function and implementation of linear table. In fact, linear table has many disadvantages. In this article, we will explain the linked list

• Concept:
Linked list is a non continuous and non sequential storage structure in physical storage structure. The logical order of data elements is through the pointer chain in the linked list
It is realized in sequence.
A linked list is like a train, which is composed of each carriage, and there are links between each carriage.
• Classification: linked list is widely used. The simplest, such as mobile phone address book, is the simplest linked list. There are actually eight types of linked lists:

The head node is also called sentry node. This node generally does not store or recommend storing data. It is specially used to facilitate you to find the first node storing valid data at any time. (it is not recommended that the header node store the length of the linked list!! because you are not sure of the node data type, such as double,char)

A two-way linked list means that each node has two pointers, one pointing to the previous node and the other pointing to the next node

The circular linked list is the tail node. The pointer is not empty, but points to the head node. Equivalent to a circle.

Then the permutation and combination of 2 * 2 * 2 is a total of eight linked list categories. Next, we will focus on single linked list and two-way circular linked list

Then we implement it with headless + one-way + acyclic linked list addition, deletion, query and modification

```typedef int Datatype;

typedef struct SlistNode
{
Datatype data;
struct SlistNode* next;
}SLTNode;
```

## 2. Capacity expansion

```SLTNode* BuySListnode(Datatype x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;
return newnode;

}
```
• Note here that the newnode node space applied in the function will not disappear with the destruction of the function stack frame. Newnode is the space opened up by malloc on the heap, so return the corresponding pointer and receive it with the pointer.

## 3. Printing

```void SListPrint(SLTNode* phead)
{
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}printf("NULL\n");
}
```
• Print normally

## 4. Insert

### Tail insertion

• Note: first of all, the parameter is the secondary pointer, that is, the primary pointer address. Once the primary pointer is received here, only the formal parameters of the primary pointer in the function are inserted and changed, which does not work in practice.
Secondly, it is discussed in two cases:
1) If the header pointer is null, directly give the new node to the header pointer
2) Normal tail insertion of multiple nodes
```void SListPushBack(SLTNode** pphead, Datatype x)
{
{
}
else
{
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}

}
```

```void SListPushFront(SLTNode** pphead, Datatype x)
{

}
```

## 5. Delete

### Tail deletion

• There are three situations to discuss: 1) the header pointer is null. 2) The header pointer is just a node. 3) Multiple nodes
```void SListPopBack(SLTNode** pphead)
{
{
return;

}
}
else{
while (p->next->next != NULL)
{
p = p->next;
}
free(p->next);
p->next = NULL;
}

}
```

```void SListPopFront(SLTNode** pphead)
{
}
```

## 6. Find

• Normal traversal looks up one by one. If equal, the first occurrence address is returned.
```SLTNode* SListFind(SLTNode* phead, Datatype x)
{
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
```

# summary

Posted by dksmarte on Mon, 25 Oct 2021 02:38:43 -0700