C++ 如何显示链接列表?
因此,我创建了以下代码,但显示函数给我带来了问题。每次我尝试使用它时,它就会变成一个无限循环。谁能看一下,告诉我出了什么问题吗C++ 如何显示链接列表?,c++,C++,因此,我创建了以下代码,但显示函数给我带来了问题。每次我尝试使用它时,它就会变成一个无限循环。谁能看一下,告诉我出了什么问题吗 #include<iostream.h> #include<conio.h> struct node{ int info; node *next; }*ptr,*start,*temp; node* create_new() { ptr=new node; cout<<"\nEnte
#include<iostream.h>
#include<conio.h>
struct node{
int info;
node *next;
}*ptr,*start,*temp;
node* create_new()
{
ptr=new node;
cout<<"\nEnter the data: ";
cin>>ptr->info;
ptr->next=NULL;
return ptr;
}
void insert_at_beg()
{
ptr=create_new();
if(start==NULL)
{
start=ptr;
}
if(start!=NULL)
{
ptr->next=start;
start=ptr;
}
}
void display()
{
temp=start;
while(temp->next!=NULL)
{
cout<<"\t"<<temp->info;
temp=temp->next;
}
}
void insert_at_end()
{
if(start==NULL)
{
start=ptr;
}
if(start!=NULL)
{
ptr=create_new();
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
}
void delete_from_end()
{
if(start==NULL)
{
cout<<"NULL LL";
}
else
{
temp=start;
while(temp->next!=NULL)
{
ptr=temp;
temp=temp->next;
}
ptr->next=NULL;
delete temp;
}
}
void delete_from_beg()
{
if(start==NULL)
cout<<"\nNULL LL";
else
start=start->next;
}
void delete_from_mid()
{
int el;
if(start==NULL)
{
cout<<"\nNULL LL";
}
else
{
cout<<"\nEnter element that you want to delete: ";
cin>>el;
temp=start;
while(temp->next!=NULL&&temp->info!=el)
{
ptr=temp;
temp=temp->next;
}
ptr->next=temp->next;
delete temp;
}
}
void main()
{
clrscr();
start=NULL;
temp=NULL;
ptr=NULL;
insert_at_beg();
display();
getch();
}
#包括
#包括
结构节点{
国际信息;
节点*下一步;
}*ptr,*启动,*温度;
节点*create_new()
{
ptr=新节点;
coutptr->info;
ptr->next=NULL;
返回ptr;
}
无效插入_在_beg()处
{
ptr=create_new();
if(start==NULL)
{
启动=ptr;
}
如果(开始!=NULL)
{
ptr->next=开始;
启动=ptr;
}
}
无效显示()
{
温度=启动;
while(临时->下一步!=NULL)
{
coutnext;
}
温度->下一步=ptr;
}
}
void从_end()中删除_
{
if(start==NULL)
{
coutnext;
}
ptr->next=NULL;
删除临时文件;
}
}
void从_beg()中删除_
{
if(start==NULL)
coutnext;
}
ptr->next=temp->next;
删除临时文件;
}
}
void main()
{
clrsc();
start=NULL;
温度=零;
ptr=NULL;
在_beg()处插入_;
显示();
getch();
}
您的错误在以下代码中:
void insert_at_beg()
{
ptr=create_new();
if(start==NULL)
{
start=ptr;
}
if(start!=NULL)
{
ptr->next=start;
start=ptr;
}
}
start
中的第一次将为空,因此您将执行start=ptr
。但是,现在如果(start!=NULL)
将为真,那么您将执行ptr->next=start
。由于ptr
和start
都指向同一个对象,您将创建一个无限循环。首先,我不建议您使用全局变量,尤其是当您的函数本身输出相同类型(和值)的参数时
我认为问题在于函数insert_at_beg():
改用else
if(start==NULL)
{
start=ptr; // now start is not NULL!!
}
else
{
ptr->next=start;
start=ptr;
}
}
此外,不是:
#include<iostream.h>
#include<conio.h>
#包括
#包括
只需使用
#include
您的导致无限循环的bug在其他地方(insert_at_beg(),我看到有人已经在代码中添加了详细信息,所以我也不会这么做)
您的代码中仍然存在问题:
void display()
{
temp=start;
while(temp->next!=NULL)
{
cout<<"\t"<<temp->info;
temp=temp->next;
}
}
您的
显示功能可以简化:
void display()
{
node * p = start;
while (p != NULL)
{
cout << "\t" << p->info;
p = p->next;
}
cout << endl; // The side effect of this is to print blank line when empty list.
}
void display()
{
节点*p=开始;
while(p!=NULL)
{
下一步;
}
在这种情况下,调试器是您最好的朋友。学习和练习使用调试器将对检测和解决您自己的错误产生巨大的影响。您的显示器不会显示最后一个节点,因为最后一个节点的next
字段将设置为NULL。
while(temp) // equivalent to while(temp !=NULL)
void display()
{
node * p = start;
while (p != NULL)
{
cout << "\t" << p->info;
p = p->next;
}
cout << endl; // The side effect of this is to print blank line when empty list.
}