单链表的简单增、删、查、改、打印(内附源码)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>


typedef int SLDataType;
typedef struct SListNode
{
	SLDataType data;
	struct SListNode* next;
}SLNode;


void SLPrint(SLNode* phead)//打印链表
{
	SLNode* puc = phead;
	while (puc)
	{
		printf("%d ", puc->data);
		puc = puc->next;
	}
	printf("NULL\n");

}

SLNode* SLBuyNode(SLDataType x)//创建新节点
{
	SLNode* p = (SLNode*)malloc(sizeof(SLNode));
	p->data = x;
	p->next = NULL;
	return p;
}


void SLPushBack(SLNode** phead, SLDataType x)//尾插
{
	assert(phead);

	SLNode* node = SLBuyNode(x);

	if (*phead == NULL)
	{
		*phead = node;
		return;
	}

	SLNode* puc = *phead;
	while (puc->next)
	{
		puc=puc->next;
	}
	puc->next = node;
	
}



void SLPushFront(SLNode** pphead, SLDataType x)//头插
{
	assert(pphead);

	SLNode* node = SLBuyNode(x);

	node->next = *pphead;
	*pphead = node;//node成为新的头节点

}


void SLPopBack(SLNode** pphead)//尾删
{
	assert(pphead);
	assert(*pphead);

	if ((*pphead)->next == NULL) 
	{
		free(*pphead);
		*pphead = NULL;
		return;
	}

	SLNode* puc = *pphead;
	while (puc->next->next != NULL)
	{
		puc = puc->next;
	}
	puc->next=NULL;

}


void SLFrontBack(SLNode** pphead)//头删
{
	assert(pphead);
	assert(*pphead);

	*pphead = (*pphead)->next;
}


//找到按照x查找节点
SLNode* SLFind(SLNode** pphead, SLDataType x)
{
	assert(pphead);

	SLNode* p = NULL;
	p = *pphead;

	while (p)
	{
		if (p->data == x)
			return p;//值给下面的 pos
		p = p->next;
	}
	return NULL;
}


//在指定位置之前插入数据
void SLInsert(SLNode** pphead, SLNode* pos, SLDataType x)
{
	assert(pphead);
	assert(*pphead);
	assert(pos);

	SLNode* node = SLBuyNode(x);
	if (pos == *pphead)//在第一个节点前插入
	{
		node->next = *pphead;
		*pphead = node;
		return;
	}

	SLNode* dev = *pphead;

	while (dev->next != pos)
	{
		dev = dev->next;
	}
	node->next = pos;
	dev->next = node;

}


//在指定位置之后插入数据
void SLInsertAfter(SLNode* pos, SLDataType x)
{
	assert(pos);

	SLNode* node = SLBuyNode(x);
	node->next = pos->next;
	pos->next = node;

}


//删除 pos 节点
void SLErase(SLNode** pphead, SLNode* pos)
{
	assert(pphead);
	assert(*pphead);
	assert(pos);

	if (pos == *pphead)
	{
		*pphead = (*pphead)->next;
		free(pos);
		pos->next = NULL;
		return;
	}

	SLNode* dev = *pphead;
	while (dev->next != pos)
	{
		dev = dev->next;
	}
	dev->next = pos->next;
	free(pos);
	pos = NULL;

}


//删除指定pos节点之后的节点
void SLEraseAfter(SLNode* pos)
{
	assert(pos);
	assert(pos->next);//pos->next要求非空
	pos->next = pos->next->next;
}


//销毁链表
void SLDestroy(SLNode** pphead)
{
	SLNode* pre = *pphead;

	while (pre)
	{
		pre = pre->next;
		free(*pphead);
		*pphead = pre;
	}
	*pphead = NULL;
}


int main()
{
	SLNode* plist = NULL;
	SLPushBack(&plist,20);//尾插
	SLPushBack(&plist,30);
	SLPushBack(&plist,40);
	SLPrint(plist);

	SLPushFront(&plist, 10);//头插
	SLPrint(plist);// 10 20 30 40 NULL

	SLPopBack(&plist);//尾删
	SLPrint(plist);// 10 20 30 NULL

	SLFrontBack(&plist);//头删
	SLPrint(plist);// 20 30 NULL
	
	SLNode* find1 = SLFind(&plist,20);
	SLInsert(&plist, find1, 10);//在find1前插入节点
	SLPrint(plist);// 10 20 30 NULL
	
	SLNode* find2 = SLFind(&plist, 30);
	SLInsertAfter(find2, 40);//在find2后插入40
	SLPrint(plist);// 10 20 30 40 NULL

	SLErase(&plist, find2);//删除find2节点
	SLPrint(plist);// 10 20 40 NULL


	SLDestroy(&plist);
	SLPrint(plist);// NULL

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值