链表的查找

对于数据结构,一直都没有怎么用,但是在面试时一直都会出现。


面试题- 链表

(1)写一个函数将一个链表逆序.

(2)一个单链表,不知道长度,写一个函数快速找到中间节点的位置.

(3)写一个函数找出一个单向链表的倒数第n个节点的指针.(把能想到的最好算法写出).


#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
struct MyStruct
{
	int data;
	MyStruct *pNext;
};

MyStruct* CreateList(int* p, int len);
MyStruct* PositiveList(MyStruct* pHead);
MyStruct* FindMidNode(MyStruct* pHead);

MyStruct* FindEndIndex(MyStruct* pHead,int nNode);

int main()
{
	int a[8] = {1,2,3,4,5,6,7,8};
	MyStruct *pHead = NULL;
	//创建链表时就让它逆序
	pHead = CreateList(a, 8);


	//逆序输出 5-1

	MyStruct *pPriData= pHead;
	while(pPriData != NULL)
	{
		cout<<"value: "<<pPriData->data<<endl;
		pPriData = pPriData->pNext;
	}
	//正序输出1-5

	cout<<endl;
	cout<<"正序输出"<<endl;
	MyStruct *pData = PositiveList(pHead);
	pPriData = pData;
	while (pPriData != NULL)
	{
		cout<<"value: "<<pPriData->data<<endl;
		pPriData = pPriData->pNext;
	}


	//查找中间的节点
	pPriData = FindMidNode(pHead);
	cout<<"位置为: "<<&pPriData<<"数据为:"<<pPriData->data<<endl;


	//查找倒数的节点的数据
	int nNode;
	cout<<"输入要查看的节点"<<endl;
	cin >>nNode;
	pPriData = FindEndIndex(pHead,nNode);
	

	system("pause");
	return 1;
}


MyStruct* FindEndIndex(MyStruct* pHead,int nNode)
{
	//先让pEnd - pBegin = nNode(他们之家的差距)
	MyStruct* pBegin = pHead;
	MyStruct* pEnd = pHead;

	//如果是1就是最后一个节点
	if(nNode == 1)
	{
			while(pBegin->pNext != NULL)
			{
				pBegin = pBegin->pNext;
			}

			return pBegin;
	}
	else
	{
		for(int i =0; i< nNode-1; i++)
		{
			pEnd= pEnd->pNext;
		}
		while(pEnd->pNext != NULL)
		{
			pEnd = pEnd->pNext;
			pBegin = pBegin->pNext;
		}
		return pBegin;
	}

}
MyStruct* FindMidNode(MyStruct* pHead)
{
	MyStruct* pSercher = pHead;
	MyStruct* pMid     = pHead;

	while(pSercher->pNext != NULL)
	{
		pSercher = pSercher->pNext->pNext;
		
		if(pSercher == NULL)
		{
			break;
		}
		pMid = pMid->pNext;
	}

	return pMid;
}


MyStruct* PositiveList(MyStruct* pHead)
{
	MyStruct *pReturn = NULL;
	MyStruct *pData = NULL;

	while(pHead != NULL)
	{
		MyStruct *pTemp = new MyStruct;
		pTemp->data = pHead->data;
		pTemp->pNext = NULL;

		if(pReturn == NULL)
		{
			pReturn = pTemp;
		}
		else
		{
			pTemp->pNext = pReturn;
			pReturn = pTemp;
		}
		pHead = pHead->pNext;
	}

	return pReturn;
}


MyStruct* CreateList(int* p,int len)
{
	MyStruct *pHead = NULL;
	MyStruct *pTemp = NULL;

	for(int i =0; i < len; i++)
	{
		pTemp = new MyStruct;
		pTemp->data = i;
		pTemp->pNext = NULL;
		if(pHead == NULL)
		{
			pHead = pTemp;
		}
		else
		{ 
			pTemp->pNext = pHead;
			pHead= pTemp;
		}
	}


	return pHead;
}

我在自己的电脑上验证通过了,如果感觉思路不好,请给我留言。您的留言是我前进的动力!


### Python 中实现链表查找元素方法 在 Python 中,可以通过定义一个 `ListNode` 类来表示单个节点,并通过遍历整个链表结构来进行元素查找操作。 #### 定义链表节点类 为了方便理解与使用,在此先给出链表节点的简单定义: ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next ``` #### 遍历链表查找特定值的方法一 可以创建一个函数用于接收头结点以及待查的目标数值作为参数,当找到该目标数时返回其索引位置;如果未发现则返回 `-1` 表明不存在这样的元素[^1]。 ```python def find(head: ListNode, target: int) -> int: """在链表查找值为 target 的首个节点""" index = 0 while head: if head.val == target: return index head = head.next index += 1 return -1 ``` #### 判断指定项是否存在于链表中的方法二 另一种方式则是直接判断某个给定的数据项是否位于当前构建好的链表内。这里采用循环的方式逐一遍历各个节点直到遇到匹配项为止,一旦成功匹配即刻终止搜索过程并返回真(`True`);反之继续前进直至到达末端仍无果,则最终返回假 (`False`)[^2]。 ```python def search(item, node: ListNode) -> bool: """查找节点是否存在""" current_node = node while current_node is not None: if current_node.val == item: return True else: current_node = current_node.next return False ``` 这两种方法都可以有效地完成链表内的元素查询工作,具体选用哪一种取决于实际应用场景的需求差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

代码i小学生

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值