在此链表中,为什么它不允许我再次 运行 并创建另一个节点我的代码中有什么错误?
In this Linked List why it is not allowing me to run again and create another node what is the error in my code?
我正在尝试使用链接列表数据结构创建员工数据库,但是一旦我输入值,运行 的选项再次不可用,并且显示功能不会在我检查之前停止执行代码代码多次,但我无法发现错误。
#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;
}
以下是输出,它只允许我输入一次值,然后不询问下一个它就结束了,这里也没有执行显示功能:
PS E:\Programming\C++> cd "e:\Programming\C++\" ; if ($?) { g++ Linked_List.cpp -o Linked_List } ; if ($?) { .\Linked_List }
Enter Employee No.: 101
PS E:\Programming\C++>
您在 Link_List
构造函数中输入错误。应该是:
head=NULL;
没有
head==NULL;
更换后好像work
提示:虽然静态地用眼睛扫描代码会让你思考得更好;调试器是您需要采用的基本工具。
根据您的代码定义 Link_list 构造函数时应该是 head = NULL
。
我正在尝试使用链接列表数据结构创建员工数据库,但是一旦我输入值,运行 的选项再次不可用,并且显示功能不会在我检查之前停止执行代码代码多次,但我无法发现错误。
#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;
}
以下是输出,它只允许我输入一次值,然后不询问下一个它就结束了,这里也没有执行显示功能:
PS E:\Programming\C++> cd "e:\Programming\C++\" ; if ($?) { g++ Linked_List.cpp -o Linked_List } ; if ($?) { .\Linked_List }
Enter Employee No.: 101
PS E:\Programming\C++>
您在 Link_List
构造函数中输入错误。应该是:
head=NULL;
没有
head==NULL;
更换后好像work
提示:虽然静态地用眼睛扫描代码会让你思考得更好;调试器是您需要采用的基本工具。
根据您的代码定义 Link_list 构造函数时应该是 head = NULL
。