单链表详解

1.创建结点

在这里插入图片描述
使用malloc创建节点,然后初始化节点;放回置为节点指针;

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

2.单链表打印

在这里插入图片描述

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

3.单链表尾插

void SListPushBack(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	SListNode* newnode = BuySListNode(x);
	if ((*pplist) == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		while ((*pplist)->next != NULL)
		{
			*pplist = (*pplist)->next;
		}
		(*pplist)->next = newnode;

	}
}

4.单链表的头插

在这里插入图片描述
在这里插入图片描述

void SListPushFront(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	SListNode* newnode = BuySListNode(x);
	newnode->next = *pplist;
	*pplist = newnode;

}

5.单链表的尾删

在这里插入图片描述

void SListPopBack(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);
    //pplist指针为NULL;
	assert(*pplist);
	//单链表元素个数为1
	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	//单链表多余一个
	else
	{
		SListNode* s = *pplist;
		while (s->next->next != NULL)
		{
			s = s->next;
		}
		free(s->next);
		s->next = NULL;

	}

6.单链表头删

在这里插入图片描述

// 单链表头删
void SListPopFront(SListNode** pplist)
{
	
	//这里的一个和多个元素的使用该代码均可实现;
	    assert(*pplist);
		assert(pplist);
		SListNode* p = *pplist;
		*pplist = p->next;
		free(p);

}

7.单链表查找

在这里插入图片描述

// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{

	SListNode* s = plist;
	while (s)
	{
		if (s->data == x)
		{
			return s;
		}
		s = s->next;
	}
	return NULL;
}


** 注意**:
断言问题,需要看情况而定。
pphead必须断言:原因是他是链表指针的地址。
phead的断言:需要看是否NULL成立;如若成立则不用断言;而不成立就不断言。

8.单链表删除pos位置之后的值

在这里插入图片描述

//删除pos位置后的数;
void SListEraseAfter(SListNode* pos)
{
	//第二种
	assert(pos);
	SListNode* newnode = pos->next->next;
	free(pos->next);
	pos->next = newnode;
	//这里有两种方法:一种是将变量设为指向的地址后的一个地址,另一种是将变量改为pos后面的第二个地址;
	//第一种
	/*assert(pos);
	assert(pos->next);
	SListNode* newnode = pos->next;
	pos->next = newnode->next;
	free(newnode);*/
} 

9.删除pos位置

这个函数有两种情况:
1.当pos和pphead相等时;就是头删;
2.当pos和
pphead不相等时,就找到pos指针前的指针,再将pos后面的指针赋值给pos前元素的next;

void SListErase(SListNode** pplist, SListNode* pos)
{
	assert(pplist);
	assert(pos);
	if (*pplist == pos)
	{
		SListPopFront(pplist);
	}
	else
	{
		while ((*pplist)->next != pos)
		{
			*pplist = (*pplist)->next;
		}
		(*pplist)->next = pos->next;
		free(pos);
	}


}

10. pos之后插入

该函数较为简单,就是创建一个变量,将新变量的指针赋值给pos的next,再将新创建的元素的next赋值为pos后面的元素的地址;

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

11.pos之前插入

该函数也有两种情况;
1.第一种是:pos和pphead的指向相同;这时就是头插;
2.第二种是:pos和
pphead的指向不相同;这时需要找到pos前面元素的地址;在使用三角法来进行加;

三角法
在这里插入图片描述

void SLInsert(SListNode** pphead, SListNode* pos, SLTDateType x)
{

	assert(pphead);
	assert(pos);
	if (pos == *pphead)
	{
		//这里直接可以使用头插

		SListPushFront(pphead, x);
	}
	SListNode* newnode = BuySListNode(x);
	SListNode* a = *pphead;
	while (a->next != pos)
	{
		a = a->next;
	}	
	newnode->next = pos;
	a->next = newnode;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值