大笨鹅小喽啰 2022-06-12 21:00 采纳率: 42.9%
浏览 9

关于#链表#的问题,如何解决?

单链表查找值

#include
using namespace std;
struct node
{
int num;
node *next;
};
void set_ahead(node *head)
{
int n;
cin>>n;
while(n--)
{
node *p=new node;
cin>>p->num;
p->next=head->next;
head->next=p;
}
node *tmp=head->next;
while(n--)
{
cout<num<<' ';
tmp=tmp->next;
}
}
void found(node *head)
{
int goal;
cin>>goal;
node *pt=head->next;
while(pt)
{
if(pt->num==goal)
cout<<"Found!"<<endl;
else
pt=pt->next;
}
cout<<"Not found!"<<endl;
}
int main()
{
node *head=new node;
set_ahead(head);
found(head);
return 0;
}

img

为啥无法执行found函数,求帮忙看看,谢谢!

  • 写回答

2条回答 默认 最新

  • Zz...,. 2022-06-12 21:57
    关注

    set head中第二个n-- 的n没有初始化

    评论

报告相同问题?

问题事件

  • 创建了问题 6月12日

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样