C语言SList链表的多功能的实现

//SList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// slist.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
// 分析思考为什么不在pos位置之前插入?



void SListInsert(SListNode** pplist,SListNode* pos, SLTDateType x);//在pos之前插入值x
void SListInsertAfter(SListNode* pos, SLTDateType x);//在pos之后插入值x


// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置?



void SListErase(SListNode* pos);//删除pos位置的值
void SListEraseAfter(SListNode* pos);//删除pos之后的值


// 单链表的销毁
void SListDestory(SListNode** pplist);
//SList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"

SListNode* BuySListNode(SLTDateType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}



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


void SListPushBack(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	SListNode* newnode = BuySListNode(x);
	SListNode* tail = *pplist;
	//结构体为空的时候
	if (*pplist == NULL)
	{
		*pplist = newnode;
	}
	//结构体不为空时
	else
	{
		while (tail->next)
		{
			tail=tail->next;
		}
		tail->next = newnode;
	}
}


void SListPushFront(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	SListNode* newnode = BuySListNode(x);
	//结构体空不空都是一样是这个
		newnode->next = *pplist;
		*pplist = newnode;
}



void SListPopBack(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);//没有节点不能删
	SListNode* tail = *pplist;
	if ((*pplist)->next == NULL)//一个节点
	{
		free(*pplist);
		*pplist = NULL;
	}
	else //多个节点
	{
		while (tail->next->next != NULL)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}


void SListPopFront(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);//没有节点不能删
	//一个节点和多个节点是一样的
	SListNode* next = (*pplist)->next;
	free(*pplist);
	*pplist = next;
}


SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	SListNode* cur = plist;
	while (cur)
	{
		if (cur->data != x)
			cur = cur->next;
		else
			return cur;
	}
	return NULL;
}



void SListInsert(SListNode** pplist,SListNode* pos, SLTDateType x)
{
	assert(pplist);
	assert(pos);//保证传入的pos不为空,间接上也保证结构体不为空,空的结构体无法用该insert,至少得含有一个节点,在此节点前插入
	SListNode* newnode = BuySListNode(x);
	SListNode* prev = *pplist;
	
	//没有节点(没有节点的实现不了实际上,因为pos传进来不允许为NULL)和一个节点的时候
	if (pos == * pplist)
	{
		SListPushFront(pplist, x);
	}


	//多个节点的时候
	else
	{
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = newnode;
		newnode->next = pos;
	}
}



void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode* del = pos->next;
	SListNode* newnode = BuySListNode(x);
	pos->next = newnode;
	newnode->next = del;
}

void SListErase(SListNode** pplist,SListNode* pos)
{
	assert(pplist);
	assert(*pplist);//保证了结构体不是空
	assert(pos);//保证了传入的坐标不为NULL,间接上也保证了结构体不为空
	SListNode* cur = *pplist;
	if (*pplist == pos)//单个节点的时候
	{
		SListPopFront(pplist);
	}
	else//多个节点的时候
	{
		while (cur)
		{
			if (cur->next == pos)
			{
				SListNode* del = cur->next->next;
				free(cur->next);
				cur->next = del;
			}
			else
			{
				cur = cur->next;
			}
		}
	}
}


void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	assert(pos->next);//确保了结构体是多个节点
	
	//多个节点
	SListNode* del = pos->next->next;
	free(pos->next);
	pos->next = del;
}

void SListDestory(SListNode** pplist)
{
	assert(*pplist);
	assert(pplist);
	while (*pplist)
	{
		SListNode* next = (* pplist)->next;
		free(*pplist);
		*pplist = next;
	}
}

//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"



void test6()
{
	SListNode* n1 = BuySListNode(1);
	SListNode* n2 = BuySListNode(2);
	SListNode* n3 = BuySListNode(3);
	SListNode* n4 = BuySListNode(4);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	SListNode* head = n1;
	SListPrint(head);
	SListNode* plist = n1;
	SListNode* cur = SListFind(plist, 2);//找到2的地址
	if (cur != NULL)
	{
		printf("%d\n", cur->data);//打印2
	}
	else
	{
		printf("没找到\n");
	}
	SListEraseAfter(cur);//删除2后面的
	SListPrint(head);
	SListDestory(&head);
	SListPrint(head);//销毁链表

}

void test5()
{
	SListNode* n1 = BuySListNode(1);
	SListNode* n2 = BuySListNode(2);
	SListNode* n3 = BuySListNode(3);
	SListNode* n4 = BuySListNode(4);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	SListNode* head = n1;
	SListPrint(head);
	SListNode* plist = n1;
	SListNode* cur = SListFind(plist, 2);//找到2的地址
	if (cur != NULL)
	{
		printf("%d\n", cur->data);//打印2
	}
	else
	{
		printf("没找到\n");
	}
	SListInsertAfter(cur, 8);//在2之后插入8
	SListPrint(head);
	
}

void test4()
{
	SListNode* n1 = BuySListNode(1);
	SListNode* n2 = BuySListNode(2);
	SListNode* n3 = BuySListNode(3);
	SListNode* n4 = BuySListNode(4);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	SListNode* head = n1;
	SListPrint(head);
	SListNode* plist = n1;
	SListNode* cur = SListFind(plist, 2);//找到2的地址
	if (cur != NULL)
	{
		printf("%d\n", cur->data);//打印2
	}
	else
	{
		printf("没找到\n");
	}
	SListInsert(&plist, cur, 4);//把4插到2前
	SListPrint(head);
	SListErase(&plist, cur);//删除2
	SListPrint(head);
}


void test3()
{
	SListNode* n1 = BuySListNode(1);
	SListNode* n2 = BuySListNode(2);
	SListNode* n3 = BuySListNode(3);
	SListNode* n4 = BuySListNode(4);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	SListPrint(n1);
	SListNode* plist = n1;
	SListNode* cur=SListFind(plist, 2);
	if (cur != NULL)
	{
		printf("%d\n", cur->data);
	}
	else
	{
		printf("没找到\n");
	}

}



void test2()
{
	SListNode* n1 = BuySListNode(1);
	SListNode* n2 = BuySListNode(2);
	SListNode* n3 = BuySListNode(3);
	SListNode* n4 = BuySListNode(4);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	SListPrint(n1);
	SListNode* plist = n1;


	SListPopBack(&plist);
	SListPrint(plist);
	SListPopBack(&plist);
	SListPrint(plist);

	SListPopFront(&plist);
	SListPrint(plist);

}



void test1()
{
	SListNode* n1 = BuySListNode(1);
	SListNode* n2 = BuySListNode(2);
	SListNode* n3 = BuySListNode(3);
	SListNode* n4 = BuySListNode(4);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	 SListPrint(n1);

	 //尾插
	 SListPushBack(&n1, 5);
	 SListPushBack(&n1, 6);
	 SListPrint(n1);

	 //头插
	 SListPushFront(&n1, 7);
	 SListPushFront(&n1, 8);
	 SListPrint(n1);

}





int main()
{
	test1();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	test2();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	test3();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	test4();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	test5();
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	test6();
	return 0;
}

运行图片(根据void test对应):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值