C语言——不带哨兵位不循环单链表详解

在这里详细讲解单链表的增删查改等基本操作。

假设单链表存储数据是int类型的,将它重命名为SLDataType。

typedef int SLDataType;

单链表中每个节点包含它的有效数据以及指向下一个结点的指针,将这个结构体重命名为SList。

typedef struct SListNode
{
	SLDataType data;
	struct SListNode* next;
}SList;

在头文件中声明要实现功能的函数

//声明函数

//创建新结点
SList* CreateNode(SLDataType x);

//头插
void SListPushFront(SList** pphead, SLDataType x);
 
//尾插
void SListPushBack(SList** pphead, SLDataType x);

//头删
void SListPopFront(SList** pphead);

//尾删
void SListPopBack(SList** pphead);

//打印单链表
void PrintSList(SList* phead);

//查找链表中的结点
SList* SListFind(SList* phead, SLDataType x);

//在pos前插入结点
void SListInsert(SList** pphead, SList* pos, SLDataType x);

//删除pos结点 
void SListErase(SList** pphead, SList* pos);

//计算结点数量
int SListCount(SList* phead);

//销毁链表
void Destory(SList** pphead);

1.创建新结点

//创建新结点
SList* CreateNode(SLDataType x)
{
	SList* newnode = (SList*)malloc(sizeof(SList));

	if (newnode == NULL)
	{
		printf("malloc newnode fail\n");
		exit(-1);
	}

	//初始化新结点
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}

这个模块在头插尾插中间插都要使用,因此放在第一个,使用这个函数,将有效数据传过去,返回一个新开辟结点的指针。

2.头插

//头插
void SListPushFront(SList** pphead, SLDataType x)
{
	SList* newnode = CreateNode(x);
	
	newnode->next = *pphead;
	*pphead = newnode;
}

这里要注意,在源文件中创建链表头结点的是一个指针,传参要传这个指针的地址,因为在这里会改变头结点的地址,因此使用二级指针,这样保证在函数改变实参指针也能改变形参的指针,在后面的一系列操作中大部分都会用到二级指针,这点很重要,并且,在这里,不管头指针是否为空,都可以保证程序正常进行。

3.打印单链表

//打印单链表
void PrintSList(SList* phead)
{
	SList* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

 因为在这个模块并不会改变头结点的指针,因此只需要传一级指针就可以了,遍历链表,打印有效数据,直到尾结点。

4.尾插

//尾插
void SListPushBack(SList** pphead, SLDataType x)
{
	SList* newnode = CreateNode(x);

	//1.没有节点
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	//2.只有一个节点
	else if ((*pphead)->next == NULL)
	{
		(*pphead)->next = newnode;
	}
	//3.多个节
	else
	{
		SList* cur = *pphead;
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		cur->next = newnode;
	}
}

在尾插中要注意分类讨论,写出bug很多是因为有不同种情况,函数只写了部分情况导致非法访问等情况。如果链表是空的话直接将新结点的指针赋给头结点指针即可,如果有一个结点的话就连在它的后面,如果有多个节点的话需要遍历链表找到尾结点,再连接起来即可。

5.头删

//头删
void SListPopFront(SList** pphead)
{
	//1.没有节点
	if (*pphead == NULL)
	{
		return;
	}
	//2.只有一个节点
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
		return;
	}
	//3.多个节点
	else
	{
		SList* next = (*pphead)->next;
		free(*pphead);
		*pphead = next;
	}
}

 这里也要分类讨论,如果头结点为空,就是说没有可以删的元素,直接返回。如果有一个节点,直接free掉头结点,再将指针置为空即可。如果有多个结点的话,先记住第二个结点,free掉第一个,使头指针指向第二个结点就可以了。事实上一个节点和多个节点可以写成一个,在这里主要是为了提醒分类讨论的重要性。

6.尾删

//尾删
void SListPopBack(SList** pphead)
{
	//1.没有节点
	if (*pphead == NULL)
	{
		return;
	}
	//2.只有一个节点
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
		return;
	}
	else
	{
		SList* prev = NULL;
		SList* cur = *pphead;
		while (cur->next != NULL)
		{
			prev = cur;
			cur = cur->next;
		}
		prev->next = NULL;
		free(cur);
	}
}

 这里也可能会改变头指针的地址,因此也要传二级指针。如果头指针指向空,直接返回。如果只有一个结点,free掉这个节点,再将头指针指向空就行了。如果多个节点的话,需要遍历链表,找到最后一个结点和它前面的结点,因此在这里最好设置两个指针,一前一后,最终找到这两个结点,free掉tail指向的结点,再将prev的next置为空就行了。

 7.查找链表中的结点

//查找链表中的结点
SList* SListFind(SList* phead, SLDataType x)
{
	if (phead == NULL)
		return NULL;

	SList* cur = phead;
	while (cur)
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}

	return NULL;
}

传过来一个数据,遍历链表,找到这个数据,返回它的地址,如果找不到就返回NULL。

8.再pos前插入结点

//在pos前插入结点
void SListInsert(SList** pphead, SList* pos, SLDataType x)
{
	if (*pphead == NULL)
	{
		return;
	}

	//如果要插在头结点前面,直接调用头插
	if (pos == *pphead)
	{
		SListPushFront(pphead, x);
		return;
	}

	SList* newnode = CreateNode(x);
	SList* cur = *pphead;
	while (cur->next != pos)
	{
		cur = cur->next;
	}
	cur->next = newnode;
	newnode->next = pos;
}

如果要插在第一个地方,直接调用头插即可。插在中间的话也是要遍历链表,找到pos前面的结点,将新结点插在他后面,将新结点的next指向pos即可。

9.删除pos结点

//删除pos结点 
void SListErase(SList** pphead, SList* pos)
{
	if (*pphead == NULL)
	{
		return;
	}

	//如果要删除头结点,直接调用头删
	if (pos == *pphead)
	{
		SListPopFront(pphead);
		return;
	}

	SList* prev = NULL;
	SList* cur = *pphead;
	while (cur != pos)
	{
		prev = cur;
		cur = cur->next;
	}
	prev->next = cur->next;
	free(cur);
}

 如果要删除的位置在第一个,直接调用头删函数即可。删除中间结点的话,就是要找到pos结点和它前面的结点prev,将prev的next指向pos的next,再将pos位置的结点free掉即可。

10.计算结点数目

//计算结点数量
int SListCount(SList* phead)
{
	int count = 0;
	SList* cur = phead;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}

 这个就没什么好讲的,创建count变量计算,遍历链表,返回count就行了。

11.销毁链表

//销毁链表
void Destory(SList** pphead)
{
	if (*pphead)
		return;

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

这一步很重要,一定要销毁链表,防止内存泄漏。记住下一个结点,free当前节点,再使cur=next,直到全都free掉,记住将头指针指向空。

接下来是这些模块的测试用例以及结果

void test1()
{
	//测试头插
	SList* head = NULL;

	SListPushFront(&head, 1);
	SListPushFront(&head, 2);
	SListPushFront(&head, 3);
	SListPushFront(&head, 4);
	PrintSList(head);

	Destory(&head);
}

void test2()
{
	//测试尾插
	SList* head = NULL;

	SListPushBack(&head, 1);
	SListPushBack(&head, 2);
	SListPushBack(&head, 3);
	SListPushBack(&head, 4);
	PrintSList(head);

	Destory(&head);
}

void test3()
{
	//测试头删
	SList* head = NULL;

	SListPushBack(&head, 1);
	SListPushBack(&head, 2);
	SListPushBack(&head, 3);
	SListPushBack(&head, 4);
	SListPushBack(&head, 5);
	SListPushBack(&head, 6);
	SListPushBack(&head, 7);
	SListPushBack(&head, 8);
	PrintSList(head);

	SListPopFront(&head);
	SListPopFront(&head);
	SListPopFront(&head);
	PrintSList(head);

	SListPopFront(&head);
	SListPopFront(&head);
	SListPopFront(&head);
	SListPopFront(&head);
	SListPopFront(&head);
	SListPopFront(&head);
	PrintSList(head);

	Destory(&head);
}

void test4()
{
	//测试尾删
	SList* head = NULL;

	SListPushBack(&head, 1);
	SListPushBack(&head, 2);
	SListPushBack(&head, 3);
	SListPushBack(&head, 4);
	SListPushBack(&head, 5);
	SListPushBack(&head, 6);
	SListPushBack(&head, 7);
	SListPushBack(&head, 8);
	PrintSList(head);

	SListPopBack(&head);
	SListPopBack(&head);
	SListPopBack(&head);
	PrintSList(head);

	SListPopBack(&head);
	SListPopBack(&head);
	SListPopBack(&head);
	SListPopBack(&head);
	SListPopBack(&head);
	SListPopBack(&head);
	PrintSList(head);

	Destory(&head);
}

void test5()
{
	//测试插入中间
	SList* head = NULL;

	SListPushBack(&head, 1);
	SListPushBack(&head, 2);
	SListPushBack(&head, 3);
	SListPushBack(&head, 4);
	PrintSList(head);

	SList* pos = SListFind(head, 2);
	if (pos)
	{
		SListInsert(&head, pos, 5);
	}
	PrintSList(head);

	pos = SListFind(head, 1);
	if (pos)
	{
		SListInsert(&head, pos, 10);
	}
	PrintSList(head);

	pos = SListFind(head, 4);
	if (pos)
	{
		SListInsert(&head, pos, 40);
	}
	PrintSList(head);

	Destory(&head);
}

void test6()
{
	//测试删除中间
	SList* head = NULL;

	SListPushBack(&head, 1);
	SListPushBack(&head, 2);
	SListPushBack(&head, 3);
	SListPushBack(&head, 4);
	SListPushBack(&head, 5);
	SListPushBack(&head, 6);
	SListPushBack(&head, 7);
	SListPushBack(&head, 8);
	PrintSList(head);

	SList* pos = SListFind(head, 5);
	SListErase(&head, pos);
	PrintSList(head);

	pos = SListFind(head, 1);
	SListErase(&head, pos);
	PrintSList(head);

	pos = SListFind(head, 8);
	SListErase(&head, pos);
	PrintSList(head);

	Destory(&head);
}

void test7()
{
	SList* head = NULL;

	SListPushBack(&head, 1);
	SListPushBack(&head, 2);
	SListPushBack(&head, 3);
	SListPushBack(&head, 4);
	SListPushBack(&head, 5);
	SListPushBack(&head, 6);
	SListPushBack(&head, 7);
	SListPushBack(&head, 8);
	PrintSList(head);

	printf("%d\n", SListCount(head));

	Destory(&head);
}

int main()
{
	test1();
	test2();
	test3();
	test4();
	test5();
	test6();
	test7();
	
	return 0;
}

 

 可以将他们一一比对,一边更好理解

以上就是单链表的一些基本操作。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值