第三章:单链表

一、创建一个结点

typedef int SLTType;
typedef struct SLTnode
{
	SLTType data;
	struct SLTNode* next;
}SLTNode;

二、创建一个链表

	//开辟空间
	SLTNode* n1 = (SLTNode*)malloc(sizeof(SLTNode));
	assert(n1);
	SLTNode* n2 = (SLTNode*)malloc(sizeof(SLTNode));
	assert(n2);
	SLTNode* n3 = (SLTNode*)malloc(sizeof(SLTNode));
	assert(n3);
	SLTNode* n4 = (SLTNode*)malloc(sizeof(SLTNode));
	assert(n4);
	//给链表赋值
	n1->data = 1;
	n2->data = 2;
	n3->data = 3;
	n4->data = 4;

	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;

	SLTNode* SList = NULL;

三、打印链表

void SLTprint(SLTNode* phead)
{
	SLTNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

四、开辟一个新的结点

SLTNode* SLTCreat(SLTType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}

五、尾插

void SLTPushBack(SLTNode** phead, SLTType x)
{
	assert(phead);
	SLTNode*newnode=SLTCreat(x);
	SLTNode* tail = *phead;
	if (*phead == NULL)
	{
		*phead = newnode;
	}
	else
	{
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

六、头插

void SLTPushFront(SLTNode** phead, SLTType x)
{
	assert(phead);
	SLTNode* newnode = SLTCreat(x); 
	newnode->next = *phead;
	*phead = newnode;
}

七、尾删

void SLTPopBack(SLTNode** phead)
{
	assert(phead);
	assert(*phead);
	SLTNode* tail = *phead;
	SLTNode* retail = NULL;
	if ((*phead)->next == NULL)
	{
		*phead = NULL;
	}
	else
	{
		while (tail->next != NULL)
		{
			retail = tail;
			tail = tail->next;
		}
		free(tail);
		retail->next = NULL;
	}
}

八、头删

void SLTPopFront(SLTNode** phead)
{
	assert(phead);
	assert(*phead);
	SLTNode* x = *phead;
	*phead = (*phead)->next;
	free(x);
}

九、查找

SLTNode* SLTFind(SLTNode* phead, SLTType x)
{
	SLTNode* cur = phead;
	while (cur != NULL)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

十、插入

SLTNode* SLTInsert(SLTNode** phead, SLTNode* pos, SLTType x)
{
	assert(pos);
	assert(phead);
	if (pos == *phead)
	{
		SLTPushFront(phead, x);
	}
	else
	{
		SLTNode* newnode = SLTCreat(x);
		SLTNode* cur = *phead;
		while (cur -> next != pos)
		{
			cur = cur->next;
		}
		cur->next = newnode;
		newnode->next = pos;
	}
}

十一、删除

SLTNode* SLTDele(SLTNode** phead, SLTNode* pos)
{
	assert(phead);
	assert(*phead);
	if (pos == *phead)
	{
		SLTPopFront(phead);
	}
	else
	{
		SLTNode* cur = *phead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
	}
}

总结

看到这里不要以为就结束了,赶快点击到下一章的习题练习检验自己会不会吧!!

如果觉得浩克写的还不错的话,记得关注浩克奥,我们一起努力!
在这里插入图片描述

  • 22
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无敌浩克 .

感谢您对浩克的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值