单链表的使用方法.数据结构(三)[下]

前言

提示:文本为数据解构(三)[上]后续补充文:

本文具体讲解顺序表的具体使用方法


提示:以下是本篇文

系列文章目录

第一章 数据解构(一)

第二章 顺序表的具体使用方法.数据解构(二) 

第二章 单链表的使用方法.数据结构(三)[上]


文章目录

目录

前言

系列文章目录

文章目录

一、单链表使用方式

1.查找

2.查找多个值

3.修改

4.pos位置插入x


提示:以下是本篇文章正文内容,下面案例可供参考

回顾上文说到

头插,尾插,头删,尾删,扩容,输出

一、单链表使用方式

1.查找

逻辑如下(示例):

 代码如下(示例):

List* SLFind(List* plist,SLDataType x)
{
	List* find = plist;
	while (find->next != NULL)
	{
		if (find->data == x)
		{
			return find;
		}
		else
		{
			find = find->next;
		}
	}
	if (find==NULL)
	{
		printf("找不到\n");
	}
}

2.查找多个值

逻辑如下(示例):

 代码如下(示例):

void SLFinds(List* plist, SLDataType x)
{
	List* Finds = SLFind(plist, x);
	int i = 1;
	while (Finds)
	{
		if (Finds->data == x) 
		{
			printf("第%d个%d\n", i++, Finds->data);
		}
		Finds = Finds->next;
	}
}

3.修改

逻辑如下(示例):

 代码如下(示例):

//修改
void SLAmend(List* plist, SLDataType x,SLDataType y)
{
	List* amend = SLFind(plist, x);
	if (amend)
	{
		amend->data = y;
	}
}

4.pos位置插入x

 逻辑如下(示例):

代码如下(示例):

List* POS = SLFind(str,1);
	if (POS)
	{
		SLInsertPos(&str, POS, 90);
	}
void SLInsertPos(List** pplist, List* pos, SLDataType x)
{
	List* newlist = SListAdd(x);//新的节点
	if (*pplist == pos)//判断是不是第一个
	{
        //头加
		newlist->next = *pplist;
		*pplist = newlist;
	}
	else
	{
		List* find = *pplist;
		while (find->next != pos)
		{
			find = find->next;
		}
		find->next = newlist;
		newlist->next = pos;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_45591898/article/details/128793513