我正在尝试使用链表数据结构创建员工数据库,但是一旦我输入该值,再次运行的选项将不可用,并且显示功能也不会在我检查代码之前停止执行代码sevearl 次,但我无法发现错误。
#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;
它似乎工作,更换后。
提示:虽然用眼睛静态扫描代码会让你思考得更好;调试器是您需要采用的基本工具。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句