单链表的插与删基本操作(C语言)

函数声明:

#include <stdio.h>
#include <Windows.h>
#include <assert.h>

typedef int DataType; 

typedef struct SListNode 
{ 
	struct SListNode* next; 
	DataType data; 
}SListNode; 

SListNode* BuySListNode(DataType x);//创建节点
void SListPrint(SListNode* pHead); //打印链表
void SListDestory(SListNode** ppHead); //释放链表

void SListPushBack(SListNode** ppHead, DataType x);//尾插 
void SListPopBack(SListNode** ppHead); //尾删
void SListPushFront(SListNode** ppHead, DataType x);//头插 
void SListPopFront(SListNode** ppHead);//头删
SListNode* SListFind(SListNode* pHead, DataType x);//寻找一个节点 
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x);//在某个位置插入 
void SListErase(SListNode** ppHead, SListNode* pos); //删除某个位置

函数定义:

SListNode* BuySListNode(DataType x)
{
	SListNode *node=(SListNode*)malloc(sizeof(SListNode));
	node->data=x;
	node->next=NULL;
	return node;
}
void SListPrint(SListNode* pHead)
{
	SListNode* cur=pHead;
	while (cur!=NULL)
	{
		printf("%d ",cur->data);
		cur=cur->next;
		
	}
	printf("\n");
}
void SListPushBack(SListNode** ppHead, DataType x)
{
	assert(ppHead);
	if (*ppHead==NULL)
	{
		*ppHead=BuySListNode(x);
	}
	else
	{
		SListNode *cur=*ppHead;
		while(cur->next)
		{
			cur=cur->next;
		}
		cur->next=BuySListNode(x);
	}
}
void SListPopBack(SListNode** ppHead)
{
	assert(ppHead);
	if (*ppHead==NULL)
	{
		return;
	}
	else if ((*ppHead)->next==NULL)
	{
		free(*ppHead);
		*ppHead=NULL;
	} 
	else
	{
		SListNode *prev=NULL;
		SListNode *cur=*ppHead;
		while (cur->next)
		{
			prev=cur;
			cur=cur->next;
		}
		prev->next=NULL;
		free(cur);
	}
}
void SListPushFront(SListNode** ppHead, DataType x)
{
	assert(ppHead);
	if (*ppHead==NULL)
	{
		*ppHead=BuySListNode(x);
	}
	else
	{
		SListNode *cur=BuySListNode(x);
		cur->next=*ppHead;	
		*ppHead=cur;
	}
}
void SListPopFront(SListNode** ppHead)
{
	assert(ppHead);
	if (*ppHead==NULL)
	{
		return;
	}
	else if ((*ppHead)->next==NULL)
	{
		free(*ppHead);
		*ppHead=NULL;
	}
	else
	{
		SListNode *cur=*ppHead;
		cur=cur->next;
		free(*ppHead);
		*ppHead=cur;
	}
}
SListNode* SListFind(SListNode* pHead, DataType x)
{	SListNode *cur=pHead;
	assert(pHead);

	while(cur)
	{
		if (cur->data==x)
		{
			return cur;
		}
		cur=cur->next;
	}
	return NULL;
}
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)
{
	assert(ppHead&&pos);
	if (pos==*ppHead)
	{
		SListPushFront(ppHead,x);
	}
	else
	{
		SListNode *cur=BuySListNode(x);
		SListNode *prev=*ppHead;
		while (prev->next!=pos)
		{
			prev=prev->next;
		}
		prev->next=cur;
		cur->next=pos;

	}
}
void SListErase(SListNode** ppHead, SListNode* pos)
{
	assert(ppHead&&pos);
	if (pos==*ppHead)
	{
		SListPopFront(ppHead);
	} 
	else
	{
		SListNode *cur=*ppHead;
		while (cur->next!=pos)
		{
			cur=cur->next;
		}
		cur->next=pos->next;
		free(pos);
	}
}
void SListDestory(SListNode** ppHead)
{
	assert(ppHead);
	if ((*ppHead)->next==NULL)
	{
		free(ppHead);
		*ppHead=NULL;
	} 
	else
	{
		SListNode *cur=*ppHead;
		while (cur->next!=NULL)
		{
			SListPopBack(ppHead);
		}
	}
}

测试代码:

void Test1()//测试尾插头删尾删
{
	SListNode *a=NULL;
	SListPushBack(&a,1);
	SListPushBack(&a,2);
	SListPushBack(&a,3);
	SListPopBack(&a);
	SListPopFront(&a);
	SListPrint(a);
}
void Test2()//测试头插头删尾删
{
	SListNode *a=NULL;
	SListPushFront(&a,1);
	SListPushFront(&a,2);
	SListPushFront(&a,3);
	SListPopBack(&a);
	SListPopFront(&a);
	SListPrint(a);
}
void Test3()//测试查找,某个位置删,插
{
	SListNode *a=NULL;
	SListNode *pos=NULL;
	SListPushFront(&a,1);
	SListPushFront(&a,2);
	SListPushFront(&a,3);
	pos=SListFind(a,2);
	SListInsest(&a,pos,0);
	SListPrint(a);
	SListErase(&a,pos);
	SListPrint(a);
}

主函数:

int main()
{
	Test1();
	Test2();
	Test3();

	system("pause");
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值