单链表的增删查改

目录

一、主函数(test.c)

二、头文件(slist.h)

三、函数调用(slist.c)

一、主函数(test.c)

cs 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include "slist.h"

int main()
{
	SListNode* plist = NULL;

	//尾插
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPushBack(&plist, 5);
	//打印链表
	SListPrint(plist);
	//头插
	SListPushFront(&plist, 0);
	SListPushFront(&plist, 1);
	SListPushFront(&plist, 2);
	SListPushFront(&plist, 3);
	SListPushFront(&plist, 4);
	SListPushFront(&plist, 5);
	SListPrint(plist);
	//尾删
	SListPopBack(&plist);
	SListPrint(plist);
	//头删
	SListPopFront(&plist);
	SListPrint(plist);
	//查找
	SListNode* search = SListFind(plist, 2);
	SListPrint(search);
	//先查找位置在删除或者插入
	//在pos后插入x
	//SListInsertAfter(&plist, search, 10);
	//SListPrint(plist);
	//删除pos位置之后的值
	//SListEraseAfter(plist, 10);

	return 0;
}

二、头文件(slist.h)

cs 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.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** pplist,SListNode* pos, SLTDateType x);
// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos);

三、函数调用(slist.c)

cs 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include "slist.h"

//在动态内存申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

//单链表打印
void SListPrint(SListNode* plist)
{
	if (plist == NULL)
	{
		return 1;
	}
	SListNode* tail = plist;
	while (tail != NULL)
	{
		printf("->%d", tail->data);
		tail = tail->next;
	}
	printf("\n");
}

//单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{
	SListNode* addnode = BuySListNode(x);
	if (*pplist == NULL)
	{
		*pplist = addnode;
	}
	//找尾
	else
	{
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = addnode;
	}
}

 //单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	SListNode* addnode = BuySListNode(x);
	if (*pplist == NULL)
	{
		*pplist = addnode;
	}
	else
	{
		SListNode* tail = *pplist;
		*pplist = addnode;
		(*pplist)->next = tail;
	}
}


//单链表尾删
void SListPopBack(SListNode** pplist)
{
	assert(*pplist != NULL);

	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
		SListNode* tail = *pplist;
		SListNode* listnode = NULL;
		while (tail->next != NULL)
		{
			listnode = tail;
			tail = tail->next;
		}
		free(tail);
		tail = NULL;
		listnode->next = NULL;
	}
}


//头删
void SListPopFront(SListNode** pplist)
{
	assert((*pplist) != NULL);

	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
		SListNode* listnode = *pplist;
		*pplist = (*pplist)->next;
		free(listnode);
		listnode = NULL;
	}
}

//单链表的查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	assert(plist);

	SListNode* seardata = plist;
	//记录位置
	int count = 0;
	while (seardata != NULL)
	{
		count++;
		if (seardata->data == x)
		{
			printf("->%d在第%d个节点位置\n", x, count);
			return seardata;
		}
		else
		{
			seardata = seardata->next;
		}
	}
	printf("链表中没有这个数\n");
}

 单链表在pos位置之后插入x
//void SListInsertAfter(SListNode** pplist,SListNode* pos, SLTDateType x)
//{
//	SListNode* tail = *pplist;
//	SListNode* listnode = NULL;
//	SListNode* addnode = BuySListNode(x);
//	while (tail != pos)
//	{
//		tail = tail->next;
//		listnode = tail;
//	}
//	listnode->next = addnode;
//	addnode->next =tail;
//}

// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
{

}
相关推荐
QuantumLeap丶9 分钟前
【数据结构:从0-1】-01-数据结构介绍及学习路线规划
数据结构
云泽80812 分钟前
C/C++内存管理详解:从基础原理到自定义内存池原理
java·c语言·c++
润 下15 分钟前
C语言——深入解析C语言指针:从基础到实践从入门到精通(四)
c语言·开发语言·人工智能·经验分享·笔记·程序人生·其他
cyclel17 分钟前
散列表的小想法
算法
Code小翊21 分钟前
堆的基础操作,C语言示例
java·数据结构·算法
余俊晖21 分钟前
如何让多模态大模型学会“自动思考”-R-4B训练框架核心设计与训练方法
人工智能·算法·机器学习
Emilia486.28 分钟前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
一水鉴天33 分钟前
整体设计 逻辑系统程序 之27 拼语言整体设计 9 套程序架构优化与核心组件(CNN 改造框架 / Slave/Supervisor/ 数学工具)协同设计
人工智能·算法
小年糕是糕手43 分钟前
【数据结构】双向链表“0”基础知识讲解 + 实战演练
c语言·开发语言·数据结构·c++·学习·算法·链表
将车2441 小时前
C++实现二叉树搜索树
开发语言·数据结构·c++·笔记·学习