读书人

怎样对链表中的结点赋值?解决方案

发布时间: 2012-03-06 20:47:55 作者: rapoo

怎样对链表中的结点赋值?
需要建一个链表,链表的结点存放职工的信息,要求对结点的信息先赋值,以便遍历整

个链表.

下面是一个链表,但是运行总出现错误,望路过的高手指点一二.

#include <iostream>

#include <string>

using namespace std;

struct list

{
unsigned long int NO;
unsigned int age;
char sex[6];
list *next;
};

void creatList(list *&head);
void showList(const list *head);
void number(const list *head);
void insert(list *&head);
void out(list *&head);
void del(list *&head,list *&retirehead);

list *head=NULL;

void main()
{
list *retirehead=NULL;
creatList(head);
int choice;
cout < < "1.Show the list.\n ";
cout < < "2.Count sex number.\n ";
cout < < "3.Insert new worker 's information.\n ";
cout < < "4.Delete worker 's information.\n ";
cout < < "5.Delete 60 years old maleworker 's information or 55 years old femaleworker 's\n "
< < " information and putin another list.\n ";
cout < < "6.Show the information of retire worker.\n ";
cout < < "7.Exit.\n\n ";
loop:
cout < < "make a choice: ";
cin> > choice;
switch(choice)
{
case 1:showList(head);
break;
case 2:number(head);
break;
case 3:insert(head);
break;
case 4:out(head);
break;
case 5:del(head,retirehead);
break;
case 6:showList(retirehead);
break;
case 7:
return;
}
goto loop;
}

void creatList(list *&head)
{
list *s,*p;
s=new list;
s-> NO=123;
s-> age=21;
strcpy(s-> sex, "male ");
if(head==NULL)
head=s;
s=new list;
s-> NO=234;
s-> age=56;
strcpy(s-> sex, "female ");
p-> next=s;
p=s;
p-> next=NULL;
delete s;
s=NULL;
return;
}

void showList(const list *head)
{
if(head==NULL)
{
cout < < "there is no information of worker.\n ";
return;
}
cout < < "now the information of worker:\n ";
while(head)
{
cout < < "the NO of the worker:\n " < <head-> NO < <endl;
cout < < "the age of the worker:\n " < <head-> age < <endl;
cout < < "the sex of the worker:\n " < <head-> sex < <endl < <endl;
head=head-> next;
}
}

void number(const list *head)
{
if(NULL)
{
cout < < "there is no information of worker.\n ";
return;
}
int countMale=0,countFemale=0;
while(head)


{
if(strcmp(head-> sex, "male ")==0)
countMale++;
else
countFemale++;
head=head-> next;
}
cout < < "the number of the male worker is: " < <countMale < <endl;
cout < < "the number of the female worker is: " < <countFemale < <endl < <endl;
}

void insert(list *&head)
{
list *p,*s;
p=head;
s=new list;
while(p-> next!=NULL)
{
p=p-> next;
}
cout < < "please enter the NO of worker:\n ";
cin> > s-> NO;
cout < < "please enter the age of worker:\n ";
cin> > s-> age;
cout < < "please enter the sex of worker(male or female):\n ";
cin> > s-> sex;
while(s-> NO)
{
p-> next=s;
p=s;
s=new list;
cout < <endl;
cout < < "please enter the NO of worker:\n ";
cin> > s-> NO;
cout < < "please enter the age of worker:\n ";
cin> > s-> age;
cout < < "please enter the sex of worker(male or female):\n ";
cin> > s-> sex;
}
p-> next=NULL;
delete s;
cout < <endl;
}

void out(list *&head)
{
list *p;
unsigned long int key;
cout < < "which worker 's information will be delete:\n ";
cin> > key;
if(head==NULL)
{
cout < < "List null!\n ";
return;
}
if(head-> NO==key)
{
p=head;
head=head-> next;
delete p;
p=NULL;
cout < < "the information of NO. " < <key < < " have been delete.\n\n ";
return;
}
for(list *q=head;q-> next;q=q-> next)
{
if(q-> next-> NO=key)
{
p=q-> next;
q-> next=p-> next;
delete p;
p=NULL;
cout < < "the information of NO. " < <key < < " have been delete.\n\n ";
return;
}
}
cout < < "there is not NO. " < <key < < ".\n ";
return;
}

void del(list *&head,list *& retirehead)
{
list *p,*q,*pt;
q=new list;
for(p=head;p;p=p-> next)
if((strcmp(p-> sex, "male ")==0)&&(p-> age> 60)||(strcmp(p-> sex, "female ")==0)&&(p-> age> 55))
{
q=p;
if(retirehead==NULL)
{
retirehead=q;
}
else
pt-> next=q;
pt=q;
pt-> next=NULL;
}
delete q;
q=NULL;
}


------解决方案--------------------


建议你先学学stl,用stl的list可以极大程度简化你的代码
[解决办法]
帮顶
[解决办法]
void creatList(list *&head);
void showList(const list *head);
void number(const list *head);
void insert(list *&head);
void out(list *&head);
void del(list *&head,list *&retirehead);
改为:
void creatList(list *head);
void showList(const list *head);
void number(const list *head);
void insert(list *head);
void out(list *head);
void del(list *head,list *retirehead);
再将void creatList(list *head)的定义改为:
void creatList(list *head)
{
list *s,*p;
s=new list;
s-> NO=123;
s-> age=21;
s-> next = NULL;
strcpy(s-> sex, "male ");
if(head==NULL)
head=s;
else head-> next = s;
//s=new list;
//s-> NO=234;
//s-> age=56;
//strcpy(s-> sex, "female ");
//p-> next=s;
//p=s;
//p-> next=NULL;
//delete s;
//s=NULL;
//return;
}就行了

[解决办法]
void creatList(list *&head)
{
list *s,*p;
s=new list;
s-> NO=123;
s-> age=21;
strcpy(s-> sex, "male ");
if(head==NULL)
head=s;
s=new list;
s-> NO=234;
s-> age=56;
strcpy(s-> sex, "female ");
p-> next=s;//这里跟下面一句调换一下
p=s;
p-> next=NULL;
delete s;
s=NULL;
return;
}

读书人网 >C++

热点推荐