链表之打基础--基本操作(必会)

链表之打基础–基本操作(必会)

前言:没有系统学过链表的同学过来看看,看看链表的操作,主要是单链表,因为我们以后刷力扣的题目,也主要是单链表的题目,所以,它对我们至关重要哦;如果学过的小伙伴,可以复习复习,毕竟温故而知新,可以为所欲为(bushi)

基本操作:
1.动态申请一个节点

2.单链表打印

3.单链表尾插

4.单链表的头插

5.单链表的尾删

6.单链表头删

7.单链表查找

8.单链表在pos位置之后插入x

9.单链表的销毁

开始了哦

1.动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		exit(-1);
	}
	newnode->data = x;
	return newnode;
}
2.单链表打印
void SListPrint(SListNode* plist)
{
	if (plist == NULL)
	{
		printf("NULL\n");
		return;
	}
	else
	{
		while (plist)
		{
			printf("%d->", plist->data);
			plist = plist->next;
		}
		printf("NULL\n");
	}
}

3.单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{
	SListNode* tail = *pplist;
	SListNode* newnode = BuySListNode(x);
	newnode->next = NULL;
	if (tail == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
4.单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	SListNode* newnode = BuySListNode(x);
	newnode->next = *pplist;
	*pplist = newnode;
}
5.单链表的尾删
void SListPopBack(SListNode** pplist)
{
	assert(*pplist);
	SListNode* tail = *pplist;
	SListNode* Pretail = NULL;
	if (tail->next == NULL)
	{
		*pplist = NULL;
		return;
	}
	else
	{
		while (tail->next)
		{
			Pretail = tail;
			tail = tail->next;
		}
		free(tail);
		tail = NULL;
		Pretail->next = NULL;
	}
}
6.单链表头删
void SListPopFront(SListNode** pplist)
{
	assert(*pplist);
	SListNode* front = *pplist;
	*pplist = front->next;
	free(front);
	front = NULL;
}
7.单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	assert(plist);
	SListNode* pos = plist;
	while (pos && pos->data != x)
	{
		pos = pos->next;
	}
	return pos;
}
8.单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode* newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
9.单链表的销毁
void SListDestory(SListNode** pplist)
{
	SListNode* node = *pplist;
	SListNode* PreNode = NULL;
	while (node)
	{
		PreNode = node->next;
		free(node);
		node = PreNode;
	}
}

好啦,以上就是链表的基础操作啦,希望大家的数据结构更上一层楼!
ps:今天第一次,开车开出外地,一开好几个小时,200多公里的路程,回到家里好累好累,但也很奇怪,开车从不晕车,但不开车就晕车,why?!,一个男生晕车很丢人的啊,怎么搞的啊!!!!

  • 26
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 25
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值