大笨鹅小喽啰 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日