gpt4 book ai didi

c++ - 链表-循环去无穷大

转载 作者:行者123 更新时间:2023-11-27 23:03:16 24 4
gpt4 key购买 nike

<分区>

下面给出的是我为创建和显示链表而编写的代码。我认为显示方法导致无限循环发生,但我无法找出原因。我比较了网上的代码,看起来还不错。为什么会这样?

#include<iostream>

using namespace std;

class NODE
{
int data;
NODE*next;
NODE*start;
public:
NODE()
{
start=NULL;
}
void in(int v);
void display();
};

void NODE::in(int v)
{
NODE*n;
n=new NODE;
n->data=v;
n->next=NULL;
if (start==NULL)
{
start=n;
}
else
{
NODE*p;
p=start;
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;

}
cout<<"leaving the insert function";
delete n;
}

void NODE::display()
{
cout<<"enters the display function";
NODE*p;p=start;
cout<<"data is-"<<'\n';
while(p!=NULL)
{
cout<<p->data<<"->";
p=p->next;
}
}
int main()
{
NODE ob;
cout<<"enter the no. of values";
int h;
cin>>h;
for(int i=0;i<h;i++)
{
cout<<"enter the value to be inserted";
int v;
cin>>v;
ob.in(v);
}
ob.display();
return 0;

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com