Linked list (1) Detailed operation of single linked list


insert image description here

1. What is a linked list

The linked list is a non-sequential and non-sequential storage structure in the physical storage structure. The logical order of the data elements is realized through the link order of the pointers in the linked list.

Logical Structure:
insert image description here
Physical Structure:

insert image description here

  • The chain structure is logically continuous, but not necessarily physically continuous
  • The node is the space applied from the heap, which is allocated according to the strategy, and may be continuous or discontinuous

Second, the classification of linked lists

according toSingle and double linked listwhether there is a headWhether to cycle, the linked list can be divided intoeight

1. One-way or two-way

insert image description here

2. To lead or not to lead

insert image description here

3. To cycle or not to cycle

insert image description here

3. Implementation of headless one-way non-circular linked list

Code structure design:

  • SList.h: stores the linked list structure and the required header files, function declarations, etc.
  • SList.c: Concrete implementation of various operation functions

SList.h

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

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

// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x);
// 单链表打印
void SListPrint(SListNode* plist);
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x);
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x);
// 单链表的尾删
void SListPopBack(SListNode** pplist);
// 单链表头删
void SListPopFront(SListNode** pplist);
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x);
// 单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x);
// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos);

SList.c

#include "SList.h"

Dynamically apply for a node

SListNode* BuySListNode(SLTDateType x)
{
    
    
	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
	if (newNode == NULL)
	{
    
    
		perror("malloc fail");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;

	return newNode;
}

Singly linked list printing

void SListPrint(SListNode* plist)
{
    
    
	SListNode* cur = plist;
	while (cur != NULL)
	{
    
    
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

Single Linked List Tail Plug

void SListPushBack(SListNode** pplist, SLTDateType x)
{
    
    
	//pplist不能为空
	assert(pplist);
	//创建要插入的节点
	SListNode* newNode = BuySListNode(x);
	//链表没有节点时
	if (*pplist == NULL)
	{
    
    
		*pplist = newNode;
	}
	//链表有节点时
	else
	{
    
    
		SListNode* cur = *pplist;
		//找到链表的最后一个节点
		while (cur->next != NULL)
		{
    
    
			cur = cur->next;
		}
		cur->next = newNode;
	}
}

insert image description here

single chain plug

void SListPushFront(SListNode** pplist, SLTDateType x)
{
    
    
	//pplist不能为空
	assert(pplist);
	SListNode* newNode = BuySListNode(x);
	newNode->next = *pplist;
	*pplist = newNode;
}

insert image description here

Tail delete of singly linked list

void SListPopBack(SListNode** pplist)
{
    
    
	assert(pplist);
	//检查链表是否为空
	assert(*pplist);

	//链表只有一个节点
	if ((*pplist)->next == NULL)
	{
    
    
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
    
    
		SListNode* cur = *pplist;
		//找到倒数第二个节点
		while (cur->next->next != NULL)
		{
    
    
			cur = cur->next;
		}
		free(cur->next);
		cur->next = NULL;
	}
}

insert image description here

single chain header delete

void SListPopFront(SListNode** pplist)
{
    
    
	assert(pplist);
	//检查链表是否为空
	assert(*pplist);

	SListNode* cur = *pplist;
	*pplist = cur->next;
	free(cur);
}

insert image description here

Singly linked list lookup

SListNode* SListFind(SListNode* plist, SLTDateType x)
{
    
    
	SListNode* cur = plist;

	while (cur != NULL)
	{
    
    
		if (cur->data == x)
		{
    
    
			return cur;
		}
		cur = cur->next;
	}
	return cur;
}

Insert before pos position

void SLTInsert(SListNode** pplist, SListNode* pos, SLTDateType x)
{
    
    
	assert(pplist);
	assert(pos);

	if (pos == *pplist)
	{
    
    
		//在第一个节点前插入即头插
		SLTPushFront(pplist, x);
	}
	else
	{
    
    
		SListNode* prev = *pplist;
		while (prev->next != pos)
		{
    
    
			prev = prev->next;
		}

		SListNode* newnode = BuySListNode(x);
		prev->next = newnode;
		newnode->next = pos;
	}
}
}

insert image description here

Singly linked list inserts x after pos position

void SListInsertAfter(SListNode* pos, SLTDateType x)
{
    
    
	assert(pos);

	SListNode* newNode = BuySListNode(x);
	newNode->next = pos->next;
	pos->next = newNode;
}

insert image description here

delete pos position

void SLTErase(SListNode** pplist, SListNode* pos)
{
    
    
	assert(pplist);
	assert(pos);

	if (pos == *pplist)
	{
    
    
		//删除第一个节点即头删
		SLTPopFront(pplist);
	}
	else
	{
    
    
		SListNode* prev = *pplist;
		while (prev->next != pos)
		{
    
    
			prev = prev->next;
		}

		prev->next = pos->next;
		free(pos);
	}
}

insert image description here

Singly linked list deletes the value after the pos position

void SListEraseAfter(SListNode* pos)
{
    
    
	assert(pos);
	assert(pos->next);
	
	SListNode* cur = pos->next->next;
	free(pos->next);
	pos->next = cur;
}

insert image description here

Guess you like

Origin blog.csdn.net/zcxyywd/article/details/131993161