怎样对链表中的结点赋值?解决方案
怎样对链表中的结点赋值?
需要建一个链表,链表的结点存放职工的信息,要求对结点的信息先赋值,以便遍历整
个链表.
下面是一个链表,但是运行总出现错误,望路过的高手指点一二.
#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)
需要建一个链表,链表的结点存放职工的信息,要求对结点的信息先赋值,以便遍历整
个链表.
下面是一个链表,但是运行总出现错误,望路过的高手指点一二.
#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)