小弟初来-有问题需要请教----请多多指点
以下代码没有语法错误,编译可以通过。不过输入p之后报错。我头都大了!!!
谁来帮我啊!
#include "iostream.h"
#include "conio.h"
class node
{
public:
node();
node* createlidt(); //
node* inlist(node*head); //插入一个员工信息
node* del(node*head,int num);//删除
void showlist(node*head); //查看链表
int GetInfo(int sex);//sex=0,男职工信息;=1,女职工信息
static int total;
private:
int number;
int age;
int sex;
node*next;
};
int node::total=0;
node*head;
node::node(){number=1;age=1;sex=1;}
int node::GetInfo(int sex)
{
if(sex)cout<<"男员工信息如下:\t"<<endl;
else cout<<"女员工信息如下:\t"<<endl;
cout<<"number"<<" "<<"age"<<" "<<"sex"<<endl;
while (head&&sex==this->sex)
{cout<<head->number<<" "<<head->age<<" "<<head->sex<<endl;
head=head->next;}
cout<<endl;
return 0;
}
void node::showlist(node*head)
{
cout<<"员工信息如下:\t"<<endl;
cout<<"number"<<" "<<"age"<<" "<<"sex"<<endl;
while (head->next)
{cout<<head->number<<" "<<head->age<<" "<<head->sex<<endl;
head=head->next;}
cout<<endl;
}
node* node::createlidt()
{node*s,*p;
s=new node;
cout<<"新建数据库"<<endl;
cout<<"插入一个员工信息"<<endl;
cout<<"号码为零时结束"<<endl;
cout<<"Input number"<<endl;
cin>>s->number;
cout<<"Input age"<<endl;
cin>>s->age;
cout<<"Input sex"<<endl;
cin>>s->sex;
head=NULL;
while(s->number!=0)
{if(head==NULL)head=s;
else p->next=s;
p=s;
s=new node;
cout<<"插入一个员工信息"<<endl;
cout<<"Input number"<<endl;
cin>>s->number;
cout<<"Input age"<<endl;
cin>>s->age;
cout<<"Input sex"<<endl;
cin>>s->sex;
}
p->next=NULL;
delete s;
return(head);
}
node* node::inlist(node*head)
{
node*p,*s,*q;
s=new node;
p=head;
while(q){q->next;}
q->next=s;
cout<<"插入一个员工信息"<<endl;
cout<<"Input number"<<endl;
cin>>s->number;
cout<<"Input age"<<endl;
cin>>s->age;
cout<<"Input sex"<<endl;
cin>>s->sex;
s->next=NULL;
delete s;
return (head);
}
node* node::del(node*head,int num)
{
node *s,*p;
for (p=head;p->next;p->next)
{if(p->next->number=num)
{s=p->next;
p->next=s->next;
delete s;
cout <<num<<"号已删除"<<endl;
return head;
}
}}
int main(int argc, char* argv[])
{
node y;
node *p;
p=&y;
y.createlidt();
int code=0;
again:
cout<<"员工管理信息系统"<<endl;
//cout<<"员工总数"<<total<<endl;
char c=getch();
switch(c){
// case 's':
// y.showlist(p);
// break;
case 'i':
y.inlist(p); //插入一个员工信息
break;
case 'd':
cout<<"删除一个员工信息"<<endl;
cin>>code;
y.del(p,code); //删除一个员工信息
break;
case 'p':
y.showlist(p); //显示所有员工信息
break;
case 'g':
cout<<"男(0)或女(1)"<<endl;
cin>>code;
y.GetInfo(code);
}
goto again;
return 0;
}