C language–creation and output of headless one-way dynamic linked list

Directory

1. Create a file

2. Define the structure

3. Function interface creation

1. Dynamically apply for a node

2. Tail plug and head plug

3. Tail deletion and head deletion

4. Find

5. Insert and delete after specifying the position

6. Destroy linked list?Edit

Summarize:

4. Complete code

1 SList.h

2 SList.c

The linked list is composed of a series of nodes (each element in the linked list is called a node), and the nodes can be dynamically generated at runtime. Each node consists of two parts: one is a data field that stores data elements, and the other is a pointer field that stores the address of the next node.

This is a schematic diagram of the structure of the linked list

1. Create a file

We write the code in modules or create three files in the same way as the header file of SList.h, the function interface file of SList.c, and the test file of test.c.

Second, define the structure

3. Function interface creation

First of all, write the library functions and function definitions to be used in the header file of seqlist.h

1. Dynamically apply for a node

Before creating a linked list, you need to understand several forms of dynamic single-linked lists, headed or headless, tail insertion or head insertion. By creating linked lists multiple times, we first create a node to write a headless one-way dynamic linked list.

2. Tail plug and head plug

In order to find this linked list we create a plist pointer to this linked list

Tail insertion is to create a new node and point the next pointer of the tail node to the new node.

But we don’t know the address of the tail node, so we have to traverse the linked list to find the tail node.

The same is true for head plugging. Create a new node and let the new node point to the first node. Then point the plist pointer to the new node. The order here cannot be changed, which is wrong.

You can also create a pointer variable to hold the address of the first node and then change the pointer.

3. Tail deletion and head deletion

There are two issues to pay attention to when deleting tails. One is depending on the situation, and the other is to free up the space to be deleted.

Header deletion is relatively simple, as long as there is one element, it is the same no matter what the situation is.

4. Find

Finding is actually traversing the linked list to find the same value as the data element in the linked list and x

5. Insert and delete after specified position

The insertion and deletion after the specified position are relatively simple, as long as the address after the specified position is recorded

6. Destroy linked list

Summary:

In general, the headless single-linked list is still more troublesome, and there are better ones behind it.

4. Complete code

1 SList.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

//First typedef defines an int type to facilitate subsequent modification of the type
typedef int SLTDateType;

typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SLTNode;

// Dynamically apply for a node
SLTNode* BuySLTNode(SLTDateType x);
// single linked list print
void SLTPrint(SLTNode* pphead);
// Singly linked list end insertion
void SLTPushBack(SLTNode** pphead, SLTDateType x);
// head plug of single linked list
void SLTPushFront(SLTNode** pphead, SLTDateType x);
// tail deletion of singly linked list
void SLTPopBack(SLTNode** pphead);
// Delete the head of the single linked list
void SLTPopFront(SLTNode** pphead);
// Singly linked list lookup
SLTNode* SLTFind(SLTNode* phead, SLTDateType x);
// Singly linked list inserts x after position pos
void SLTInsertAfter(SLTNode* pos, SLTDateType x);
// Singly linked list deletes the value after pos position
void SLTEraseAfter(SLTNode* pos);
// destruction of single linked list
void SLTDestroy(SLTNode* phead);
// delete pos position
void SLTErase(SLTNode** pphead, SLTNode* pos);

2 SList.c

#include "SList.h"
// single linked list print
void SLTPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\
");
}

// Dynamically apply for a node
SLTNode* BuySLTNode(SLTDateType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
perror("malloc");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
}
// Singly linked list end insertion
void SLTPushBack(SLTNode** pphead, SLTDateType x)
{
assert(pphead);
SLTNode* newnode = BuySLTNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SLTNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
// head plug of single linked list
void SLTPushFront(SLTNode** pphead, SLTDateType x)
{
assert(pphead);
SLTNode* newhead = BuySLTNode(x);
SLTNode* head = *pphead;
*pphead = newhead;
newhead->next = head;
}
// tail deletion of singly linked list
void SLTPopBack(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* tail = *pphead;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
// Delete the head of the single linked list
void SLTPopFront(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
SLTNode* newhead = ((*pphead)->next);
free(*pphead);
*pphead = newhead;
}
// Singly linked list lookup
SLTNode* SLTFind(SLTNode* phead, SLTDateType x)
{
assert(phead);
SLTNode* tail = phead;
while (tail)
{
if (tail->data == x)
{
return tail;
}
tail = tail->next;
}
return;
}
// Singly linked list inserts x after position pos
void SLTInsertAfter(SLTNode* pos, SLTDateType x)
{
assert(pos);
SLTNode* newnode = BuySListNode(x);
pos->next = newnode;
newnode->next = pos->next;
}
// Singly linked list deletes the value after pos position
void SLTEraseAfter(SLTNode* pos)
{
assert(pos);
assert(pos->next);
SLTNode* posNext = pos->next;
pos->next = posNext->next;
free(posNext);
posNext = NULL;
}
// delete pos position
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
assert(pphead);
assert(pos);

if (pos == *pphead)
{
SLTPopFront(pphead);
}
else
{
SLTNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}

prev->next = pos->next;
free(pos);
}
}
// destruction of single linked list
void SLTDestroy(SLTNode* phead)
{
assert(phead);
SLTNode* newhead = phead->next;
SLTNode* tail = phead;
while (newhead)
{
free(tail);
tail = newhead;
if(newhead !=NULL)
newhead = newhead->next;
}
free(tail);
tail = NULL;
}