One article teaches you one-way linked list

Table of contents

1. What is a linked list?

1. Definition of linked list

2. Realization of linked list

2.1 Definition of linked list

2.2 Create a linked list

 Second, each interface of the linked list

1. Create a node

2. Head insertion (insert the newly created node into the linked list as the head)

3. Print linked list

 4. Tail insertion (insert the newly created node into the end of the linked list)

5. Head delete

 6. Tail deletion

7. Find

8. After deleting the specified node position

 9. Delete the specified location node

 10. Insert after the specified position node

 11. Insert before the specified position node

3. All codes

1. Interface header file

2. Interface implementation

3. Test


1. What is a linked list?

1. Definition of linked list

A linked list is a non-sequential and non-sequential storage structure on a physical storage unit, and the logical order of data elements is realized through the link order of pointers in the linked list. A relatively easy-to-understand statement is that each space is opened up in the computer memory, and then they are linked together through the address, and accessed through the address.

2. Realization of linked list

I only know the definition of a linked list. I guess everyone is still in the fog. I don’t know what is a linked list. Next, I will manually create a very frustrating linked list for everyone. It is not implemented in the form of a function, mainly to let everyone feel first. one time.

2.1 Definition of linked list

Before manually creating the linked list, we need to define the linked list first. The definition of the linked list, the reference of the interface function and the reference of the header file are best placed in a header file    so that when we want to use the created interface, we only need to refer to a header. The file is enough, and the implementation of the interface function can also be placed in a .c file, and finally reference the function test in another .c file, as shown in the figure: 

//链表博客版.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDateType;
//链表成员我们先用int,int简单好懂
//而之所以要给它取个SLDateType的别名
//不仅仅是因为方便和int进行区分
//更主要的是以后链表的成员不想用int类型的话
//直接在这里进行修改即可
typedef struct SlistNode
{
	SLDateType data;//成员
	struct SlistNode* next;
	//这里给它取名叫next其实是为了方便到时使用,其实你叫它abc也是可以的
	// 在链表中,一个节点通过地址链接到下一个节点,就像串串一样把它们穿起来,而这个地址则是它们唯一的联系,
	//我们这讲述的是单向链表,所以只能够是前面的找到后面的,从后面找到前面是不可能实现的。
}SlistNode;

2.2 Create a linked list

Linked list, in fact, is nothing tall, it is to find the next node through the address and then perform corresponding access. The core lies in the address    , as long as we can link the address of the first node to the next node, and link the address of the next node to the next node The address of each node.....just stop until the link is completed, here we don't link so many nodes, we simply link a linked list with 3 nodes

#include"链表博客版.h"
int main()
{
	SlistNode a, b, c;//创建三个节点
	a.next=&b;//a节点的链接部分存储b节点的地址
	b.next = &c;//b节点的链接部分存储c节点的地址
	c.next = NULL;//最后一个链接到空指针上,代表着链接结束
	a.data = 1;
	b.data = 2;
	c.data = 3;
	SlistNode* plist = &a;//将首节点保存
	while (plist)
	{
		printf("%d ", plist->data);//打印节点内的内容
		plist = plist->next;//不断地指向下一个节点,直到为空
	}
}

 Second, each interface of the linked list

1. Create a node

Creating a node is a very important function that needs to be used in the insert function. When creating a node in a function, we cannot create it directly as before. As we all know, when a node is created on a function, it will be automatically destroyed when the function exits. In order to prevent the node from being automatically destroyed, we use malloc to create the node here. Don’t forget Reference the function in the header file

#include"链表博客版.h"
SlistNode* buy_slistnode(SLDateType x)
//使用节点指针作为返回类型,来拿到创建好的新节点
{
	SlistNode* newnode = (SlistNode*)malloc(sizeof(SlistNode));
	//使用malloc创建一个新节点
	if (newnode == NULL)
	{
		perror("buy_slistnode");
		exit(-1);//创建失败直接中止程序
	}
	newnode->data = x;//将节点内容修改成需要的值
	newnode->next = NULL;//将链接对象置为空,因为不知道要链接谁
	return newnode;
}

2. Head insertion (insert the newly created node into the linked list as the head)

Why insert the head into the node first? Without him, it is much simpler than the tail plug

void slist_pushfront(SlistNode** phead, SLDateType x)
//采用二级指针的原因是,当没有节点的时候,我们要对首节点的地址进行修改
{
	//先创建一个新的节点
	SlistNode* newnode = buy_slistnode(x);
	//我们要头插是吧,也就是说新创建的节点是新的头
	//那么我们是不是应该把我们自己原来的头更新一下
	//然后再将之前的节点,也就是之前的头链接到新的头后面

/*	*phead = newnode;
	newnode->next = *phead;*/

	//但这是错误的,原因很简单,你的头更新了,那么你就找不到之前的节点了
    //换一下顺序即可
	newnode->next = *phead;
	*phead = newnode;
}

3. Print linked list

After inserting the node, I don't know whether I have inserted it or not, so let's design a function to print the linked list

void print_slist(SlistNode* phead)
{
	while (phead)//phead不为空意味着还有节点没被访问完
	{
		printf("%d->", phead->data);
		phead=phead->next;//指向下一个节点
	}
	printf("NULL\n");//访问完了打印空,提示已经访问完了
}

Test results:

#include"链表博客版.h"
void test1()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushfront(&plist, 1);//依次将1,2,3头插进链表中
	slist_pushfront(&plist, 2);//那么链表最后应该是3为头,1为尾
	slist_pushfront(&plist, 3);
	print_slist(plist);
}
int main()
{
	test1();
}

 4. Tail insertion (insert the newly created node into the end of the linked list)

Tail insertion is to be inserted at the end of the linked list, so finding the end of the linked list is a must

void slist_pushback(SlistNode** phead, SLDateType x)
{
	SlistNode* tmp = *phead;
	//创建一个首节点的拷贝,避免影响到首节点的指向
	SlistNode* newnode = buy_slistnode(x);//创建一个新节点
	while(tmp->next)
	//当成员的next为空的时候意味着已经找到目标了
	// 跳出循环
	//接下来就是把这个成员的指向改变
	{
		tmp = tmp->next;
	}
	tmp->next = newnode;
}

Many friends, when they write this, they think it has been completed, but think about it, if there is no node in the linked list at this time, that is, when *phead is NULL at this time, can you still point to next? Can you Do you dereference a null pointer? Obviously not, so we treat this case separately.

void slist_pushback(SlistNode** phead, SLDateType x)
{
	SlistNode* tmp = *phead;
	//创建一个首节点的拷贝,避免影响到首节点的指向
	SlistNode* newnode = buy_slistnode(x);//创建一个新节点
	if (*phead==NULL)//当*phead==NULL时,意味着链表为空
	{
		*phead = newnode;//直接链接
		return;
	}
	while(tmp->next)
	//当成员的next为空的时候意味着已经找到目标了
	// 跳出循环
	//接下来就是把这个成员的指向改变
	{
		tmp = tmp->next;
	}
	tmp->next = newnode;
}

Test code:

void test2()
{
	SlistNode* plist = NULL;
	slist_pushback(&plist, 10086);//依次尾插10086,666,555,111
	slist_pushback(&plist, 666);
	slist_pushback(&plist, 555);
	slist_pushback(&plist, 111);
	print_slist(plist);
}
int main()
{
  test2();
}

error condition

The program crashes directly, even if the print_slist should be printed even if it is empty, NULL is not printed

correct situation

5. Head delete

void slist_popfront(SlistNode** phead)
{
	if (*phead==NULL)//空了就别删了
	{
		printf("链表为空,操作失败\n");
		return;
	}
	SlistNode* tmp = (*phead)->next;
	//储存头的下一个节点,避免找不到
	free(*phead);//直接释放头节点
	*phead =tmp;//头节点重新指向下一个节点
}

 Effect test:

void test3()
{
	SlistNode* plist = NULL;
	slist_popfront(&plist);//直接删除,测试报错
	slist_pushback(&plist, 10086);//依次尾插10086,666,555,111
	slist_pushback(&plist, 666);
	slist_pushback(&plist, 555);
	slist_pushback(&plist, 111);
	print_slist(plist);
	slist_popfront(&plist);//删除10086
	print_slist(plist);
	slist_popfront(&plist);//删除666
	print_slist(plist);
	slist_popfront(&plist);//删除555
	print_slist(plist);
	slist_popfront(&plist);//删除111
	print_slist(plist);
}

int main()
{
	test3();
}

 6. Tail deletion

void slist_popback(SlistNode** phead)
{
	if (*phead == NULL)
	{
		printf("链表为空,操作失败\n");
		return;
	}
	if ((*phead)->next == NULL)
		//如果只有一个节点,我们就不可能找到上一个节点,因此单独处理
	{
		free(*phead);//直接释放
		*phead = NULL;
		return;
	}
	SlistNode* tmp = *phead;
	SlistNode* prev = NULL;//用来存储目标的上一个节点
	while (tmp->next)
	{
		prev = tmp;
		tmp=tmp->next;
	}
	prev->next = NULL;//改变上一个节点的指向
	free(tmp);
}

Test code:

void test4()
{
	SlistNode* plist = NULL;
	slist_popback(&plist);//直接删除,测试报错
	slist_pushback(&plist, 10086);//依次尾插10086,666,555,111
	slist_pushback(&plist, 666);
	slist_pushback(&plist, 555);
	slist_pushback(&plist, 111);
	print_slist(plist);
	slist_popback(&plist);//删除111
	print_slist(plist);
	slist_popback(&plist);//删除555
	print_slist(plist);
	slist_popback(&plist);//删除666
	print_slist(plist);
	slist_popback(&plist);//删除10086
	print_slist(plist);
	slist_popback(&plist);//删除空链表
	print_slist(plist);
}

int main()
{
	test4();
}

7. Find

Before operating on the specified location, we have to find the target location first. Finding the target location is relatively simple. Simply traverse the linked list, return the corresponding address if found, and return a null pointer if not found

SlistNode* slist_find(SlistNode* phead,SLDateType x)
{
	while (phead)
	{
		if (phead->data == x)
		{
			return phead;
		}
		phead=phead->next;
	}
	return NULL;
}

8. After deleting the specified node position

The reason why we first talk about deleting after specifying the location is because this is much simpler than deleting at the specified location

void slist_erase_after(SlistNode* pos)
{
	if (pos == NULL ||pos->next==NULL )
		//如果为空则删除失败,如果下一个节点为空也不能删除
	{
		printf("该位置无效,操作失败\n");
		return;
	}
	SlistNode* tmp = pos->next;
	pos->next = pos->next->next;
	free(tmp);
}

test code

void test5()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	print_slist(plist); 
	SlistNode* pos=slist_find(plist,1);//查找1所在的位置
	slist_erase_after(pos);//将1之后删除
	print_slist(plist);
	pos = slist_find(plist, 3);//查找3所在的位置
	slist_erase_after(pos);//将3之后删除,但是3之后没有节点,删除必定失败
	print_slist(plist);
}
int main()
{
	test5();
}

 9. Delete the specified location node

void slist_erase(SlistNode* pos,SlistNode**phead)
{
	if (pos == NULL)//为空就别删了
	{
		printf("该位置无效,操作失败\n");
		return;
	}
	if(*phead==pos)//当只有一个节点时,操作不到两个节点,单独处理
	{
		SlistNode*tmp=(*phead)->next;
		free(*phead);
		*phead = tmp;
		return;
	}
	SlistNode* cur = *phead;
	while (cur->next)
	{
		if (cur->next == pos)
		{
			break;
		}
		cur=cur->next;
	}
	//此时phead的next就是目标节点
	SlistNode* tmp = cur->next;
	cur->next = cur->next->next;//将目标节点的上一个节点链接到目标节点的下一个地址
	free(tmp);
}

Test code:

void test6()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	print_slist(plist);
	SlistNode* pos = slist_find(plist, 1);//查找1所在的位置
	slist_erase(pos, &plist);//将1删除
	print_slist(plist);	
	 pos = slist_find(plist, 2);//查找2所在的位置
	slist_erase(pos, &plist);//将2删除
	print_slist(plist);
	 pos = slist_find(plist, 3);//查找3所在的位置
	slist_erase(pos, &plist);//将3删除
	print_slist(plist);
}
int main()
{
	test6();
}

 10. Insert after the specified position node

void slist_insert_after(SlistNode* pos, SLDateType x)
{
	if (pos == NULL)
	{
		printf("目标不存在,操作失败\n");
		return;
	}
	SlistNode* newnode = buy_slistnode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

Test code:

void test7()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	SlistNode* pos = slist_find(plist, 1);//查找1所在的位置
	slist_insert_after(pos, 10086);//在1之后进行插入
	print_slist(plist);
	pos = slist_find(plist, 3);//查找3所在的位置
	slist_insert_after(pos, 520);//在3之后进行插入
	print_slist(plist);
	pos = slist_find(plist, 10086);//查找10086所在的位置
	slist_insert_after(pos,9 );//在10086之后进行插入
	print_slist(plist);
}
int main()
{
	test7();
}

 11. Insert before the specified position node

void slist_insert_before(SlistNode* pos, SLDateType x,SlistNode**phead)
{
	if (pos == NULL)
	{
		printf("目标不存在,操作失败\n");
		return;
	}	
	SlistNode* newnode = buy_slistnode(x);
	if (*phead==pos)//在第一个节点前插入,没有上一个节点,单独处理
	{
		newnode->next = pos;
		*phead = newnode;
		return;
	}
	SlistNode* cur = *phead;
	while (cur)//为空意味着找不到
	{
		if (cur->next == pos)//找到上一个节点了
		{
			break;
		}
		cur = cur->next;
	}
	if (cur == NULL)
	{
		printf("目标不存在,操作失败\n");
		return;
	}
	cur->next = newnode;
	newnode->next = pos;
}

test code

void test8()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	print_slist(plist);
	SlistNode* pos = slist_find(plist, 1);//查找1所在的位置
	slist_insert_before(pos, 10086,&plist);//在1之前插入666
	print_slist(plist);
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	pos = slist_find(plist, 10086);//查找10086所在的位置
	slist_insert_before(pos, 666, &plist);//在10086之前插入666
	print_slist(plist);
	pos = slist_find(plist, 3);//查找3所在的位置
	slist_insert_before(pos, 999, &plist);//在3之前插入999
	print_slist(plist);

}
int main()
{
	test8();
}

3. All codes

1. Interface header file

//链表博客版.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDateType;
//链表成员我们先用int,int简单好懂
//而之所以要给它取个SLDateType的别名
//不仅仅是因为方便和int进行区分
//更主要的是以后链表的成员不想用int类型的话
//直接在这里进行修改即可
typedef struct SlistNode
{
	SLDateType data;//成员
	struct SlistNode* next;
	//这里给它取名叫next其实是为了方便到时使用,其实你叫它abc也是可以的
	// 在链表中,一个节点通过地址链接到下一个节点,就像串串一样把它们穿起来,而这个地址则是它们唯一的联系,
	//我们这讲述的是单向链表,所以只能够是前面的找到后面的,从后面找到前面是不可能实现的。
}SlistNode;
SlistNode* buy_slistnode(SLDateType x);
//头插
void slist_pushfront(SlistNode**phead,SLDateType x);
//打印链表
void print_slist(SlistNode* phead);
//尾插
void slist_pushback(SlistNode** phead, SLDateType x);
//头删
void slist_popfront(SlistNode** phead);
//尾删
void slist_popback(SlistNode**phead);
//查找
SlistNode* slist_find(SlistNode*phead,SLDateType x);
//删除指定位置之后
void slist_erase_after(SlistNode*pos);
//删除指定位置
void slist_erase(SlistNode* pos,SlistNode**phead);
//在指定位置之后插入
void slist_insert_after(SlistNode* pos, SLDateType x);
//在指定位置之前插入
void slist_insert_before(SlistNode* pos, SLDateType x, SlistNode** phead);

2. Interface implementation

#include"链表博客版.h"
SlistNode* buy_slistnode(SLDateType x)
//使用节点指针作为返回类型,来拿到创建好的新节点
{
	SlistNode* newnode = (SlistNode*)malloc(sizeof(SlistNode));
	//使用malloc创建一个新节点
	if (newnode == NULL)
	{
		perror("buy_slistnode");
		exit(-1);//创建失败直接中止程序
	}
	newnode->data = x;//将节点内容修改成需要的值
	newnode->next = NULL;//将链接对象置为空,因为不知道要链接谁
	return newnode;
}
void slist_pushfront(SlistNode** phead, SLDateType x)
//采用二级指针的原因是,当没有节点的时候,我们要对首节点的地址进行修改
{
	//先创建一个新的节点
	SlistNode* newnode = buy_slistnode(x);
	//我们要头插是吧,也就是说新创建的节点是新的头
	//那么我们是不是应该把我们自己原来的头更新一下
	//然后再将之前的节点,也就是之前的头链接到新的头后面

/*	*phead = newnode;
	newnode->next = *phead;*/

	//但这是错误的,原因很简单,你的头更新了,那么你就找不到之前的节点了
    //换一下顺序即可
	newnode->next = *phead;
	*phead = newnode;
}
void print_slist(SlistNode* phead)
{
	while (phead)//phead不为空意味着还有节点没被访问完
	{
		printf("%d->", phead->data);
		phead=phead->next;//指向下一个节点
	}
	printf("NULL\n");//访问完了打印空,提示已经访问完了
}
void slist_pushback(SlistNode** phead, SLDateType x)
{
	SlistNode* tmp = *phead;
	//创建一个首节点的拷贝,避免影响到首节点的指向
	SlistNode* newnode = buy_slistnode(x);//创建一个新节点
	if (*phead==NULL)//当*phead==NULL时,意味着链表为空
	{
		*phead = newnode;//直接链接
		return;
	}
	while(tmp->next)
	//当成员的next为空的时候意味着已经找到目标了
	// 跳出循环
	//接下来就是把这个成员的指向改变
	{
		tmp = tmp->next;
	}
	tmp->next = newnode;
}
void slist_popfront(SlistNode** phead)
{
	if (*phead==NULL)//空了就别删了
	{
		printf("链表为空,操作失败\n");
		return;
	}
	SlistNode* tmp = (*phead)->next;
	//储存头的下一个节点,避免找不到
	free(*phead);//直接释放头节点
	*phead =tmp;//头节点重新指向下一个节点
}
void slist_popback(SlistNode** phead)
{
	if (*phead == NULL)
	{
		printf("链表为空,操作失败\n");
		return;
	}
	if ((*phead)->next == NULL)
		//如果只有一个节点,我们就不可能找到上一个节点,因此单独处理
	{
		free(*phead);//直接释放
		*phead = NULL;
		return;
	}
	SlistNode* tmp = *phead;
	SlistNode* prev = NULL;//用来存储目标的上一个节点
	while (tmp->next)
	{
		prev = tmp;
		tmp=tmp->next;
	}
	prev->next = NULL;//改变上一个节点的指向
	free(tmp);
}
SlistNode* slist_find(SlistNode* phead,SLDateType x)
{
	while (phead)
	{
		if (phead->data == x)
		{
			return phead;
		}
		phead=phead->next;
	}
	return NULL;
}
void slist_erase_after(SlistNode* pos)
{
	if (pos == NULL ||pos->next==NULL )
		//如果为空则删除失败,如果下一个节点为空也不能删除
	{
		printf("该位置无效,操作失败\n");
		return;
	}
	SlistNode* tmp = pos->next;
	pos->next = pos->next->next;
	free(tmp);
}
void slist_erase(SlistNode* pos,SlistNode**phead)
{
	if (pos == NULL)//为空就别删了
	{
		printf("该位置无效,操作失败\n");
		return;
	}
	if(*phead==pos)//当只有一个节点时,操作不到两个节点,单独处理
	{
		SlistNode*tmp=(*phead)->next;
		free(*phead);
		*phead = tmp;
		return;
	}
	SlistNode* cur = *phead;
	while (cur->next)
	{
		if (cur->next == pos)
		{
			break;
		}
		cur=cur->next;
	}
	//此时phead的next就是目标节点
	SlistNode* tmp = cur->next;
	cur->next = cur->next->next;//将目标节点的上一个节点链接到目标节点的下一个地址
	free(tmp);
}
void slist_insert_after(SlistNode* pos, SLDateType x)
{
	if (pos == NULL)
	{
		printf("目标不存在,操作失败\n");
		return;
	}
	SlistNode* newnode = buy_slistnode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
void slist_insert_before(SlistNode* pos, SLDateType x,SlistNode**phead)
{
	if (pos == NULL)
	{
		printf("目标不存在,操作失败\n");
		return;
	}	
	SlistNode* newnode = buy_slistnode(x);
	if (*phead==pos)//在第一个节点前插入,没有上一个节点,单独处理
	{
		newnode->next = pos;
		*phead = newnode;
		return;
	}
	SlistNode* cur = *phead;
	while (cur)//为空意味着找不到
	{
		if (cur->next == pos)//找到上一个节点了
		{
			break;
		}
		cur = cur->next;
	}
	if (cur == NULL)
	{
		printf("目标不存在,操作失败\n");
		return;
	}
	cur->next = newnode;
	newnode->next = pos;
}

3. Test

#include"链表博客版.h"
void test1()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushfront(&plist, 1);//依次将1,2,3头插进链表中
	slist_pushfront(&plist, 2);//那么链表最后应该是3为头,1为尾
	slist_pushfront(&plist, 3);
	print_slist(plist);
}
void test2()
{
	SlistNode* plist = NULL;
	slist_pushback(&plist, 10086);//依次尾插10086,666,555,111
	slist_pushback(&plist, 666);
	slist_pushback(&plist, 555);
	slist_pushback(&plist, 111);
	print_slist(plist);
}
void test3()
{
	SlistNode* plist = NULL;
	slist_popfront(&plist);//直接删除,测试报错
	slist_pushback(&plist, 10086);//依次尾插10086,666,555,111
	slist_pushback(&plist, 666);
	slist_pushback(&plist, 555);
	slist_pushback(&plist, 111);
	print_slist(plist);
	slist_popfront(&plist);//删除10086
	print_slist(plist);
	slist_popfront(&plist);//删除666
	print_slist(plist);
	slist_popfront(&plist);//删除555
	print_slist(plist);
	slist_popfront(&plist);//删除111
	print_slist(plist);
}
void test4()
{
	SlistNode* plist = NULL;
	slist_popback(&plist);//直接删除,测试报错
	slist_pushback(&plist, 10086);//依次尾插10086,666,555,111
	slist_pushback(&plist, 666);
	slist_pushback(&plist, 555);
	slist_pushback(&plist, 111);
	print_slist(plist);
	slist_popback(&plist);//删除111
	print_slist(plist);
	slist_popback(&plist);//删除555
	print_slist(plist);
	slist_popback(&plist);//删除666
	print_slist(plist);
	slist_popback(&plist);//删除10086
	print_slist(plist);
	slist_popback(&plist);//删除空链表
	print_slist(plist);
}
void test5()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	print_slist(plist); 
	SlistNode* pos=slist_find(plist,1);//查找1所在的位置
	slist_erase_after(pos);//将1之后删除
	print_slist(plist);
	pos = slist_find(plist, 3);//查找3所在的位置
	slist_erase_after(pos);//将3之后删除,但是3之后没有节点,删除必定失败
	print_slist(plist);
}
void test6()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	print_slist(plist);
	SlistNode* pos = slist_find(plist, 1);//查找1所在的位置
	slist_erase(pos, &plist);//将1删除
	print_slist(plist);	
	 pos = slist_find(plist, 2);//查找2所在的位置
	slist_erase(pos, &plist);//将2删除
	print_slist(plist);
	 pos = slist_find(plist, 3);//查找3所在的位置
	slist_erase(pos, &plist);//将3删除
	print_slist(plist);
}
void test7()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	SlistNode* pos = slist_find(plist, 1);//查找1所在的位置
	slist_insert_after(pos, 10086);//在1之后进行插入
	print_slist(plist);
	pos = slist_find(plist, 3);//查找3所在的位置
	slist_insert_after(pos, 520);//在3之后进行插入
	print_slist(plist);
	pos = slist_find(plist, 10086);//查找10086所在的位置
	slist_insert_after(pos,9 );//在10086之后进行插入
	print_slist(plist);
}
void test8()
{
	SlistNode* plist = NULL;//创建一个链表头
	slist_pushback(&plist, 1);//通过尾插依次将1,2,3头插进链表中
	print_slist(plist);
	SlistNode* pos = slist_find(plist, 1);//查找1所在的位置
	slist_insert_before(pos, 10086,&plist);//在1之前插入666
	print_slist(plist);
	slist_pushback(&plist, 2);
	slist_pushback(&plist, 3);
	pos = slist_find(plist, 10086);//查找10086所在的位置
	slist_insert_before(pos, 666, &plist);//在10086之前插入666
	print_slist(plist);
	pos = slist_find(plist, 3);//查找3所在的位置
	slist_insert_before(pos, 999, &plist);//在3之前插入999
	print_slist(plist);

}
int main()
{
	test8();
}

 This is the end of today's sharing, update the visits of all friends, I wish you a bright future O(∩_∩)O

Guess you like

Origin blog.csdn.net/fq157856469/article/details/132004241