在这个链表中,为什么它不允许我再次运行并创建另一个节点,我的代码中的错误是什么?



我试图使用链表数据结构使员工数据库,但一旦我输入值,再次运行的选项不可用,并且显示功能也不执行代码停止之前,我已经检查了代码几次,但我无法发现错误。

#include<iostream>
using namespace std;
class node
{
public:
int Emp_No;
node *next;
node()
{
next=NULL;
}
};
class Link_List
{
public:
node *head;

Link_List()
{
head==NULL;
}
void create();
void display();
};
void Link_List::create()
{
node *temp,*p;
int again;
do
{
temp=new node();
cout<<"Enter Employee No.: ";
cin>>temp->Emp_No;
if (head==NULL)
{
head=temp;
}
else
{
p=head;
while (p->next!=NULL)
{
p=p ->next;
}
p ->next=temp;
}
cout<<"Enter 1 to add more: ";
cin>>again;
} while (again==1);
}
void Link_List::display()
{
node *p1;
if (head==NULL)
{
cout<<"The linked list is empty"<<endl;
}
else
{
p1=head;
while (p1!=NULL)
{
cout<<"Employee No:"<<p1 ->Emp_No<<endl;
p1=p1->next;
}
}
}
int main()
{
Link_List emp1;
emp1.create();
emp1.display();
return 0;
}

下面是输出它只允许我输入一次值,然后不要求下一次它结束,并且这里也没有执行display函数:

PS E:ProgrammingC++> cd "e:ProgrammingC++" ; if ($?) { g++ Linked_List.cpp -o Linked_List } ; if ($?) { .Linked_List }
Enter Employee No.: 101
PS E:ProgrammingC++>

您在Link_List构造函数中有一个错别字。应该是:

head=NULL;

head==NULL;

更换后似乎还能工作。

提示:虽然用眼睛静态扫描代码可以让你更好地思考;调试器是您需要采用的基本工具。

在定义Link_list构造函数时应该是head = NULL

相关内容

最新更新