//有两个单向链表,头指针分别为list1、list2,链表中每一结点信息为姓名、基本工资,请编写程序将单向链表list1拼接在list2后面,然后输出拼接后的链表。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
char name[20];
int jbgz;
struct employee *next;
};
struct employee *Create();
struct employee*link(struct employee *list1,struct employee *list2);
int main(void)
{
struct employee *head,*p,*list1,*list2;
int size = sizeof(struct employee);
printf("Input list1's name and jbgz:\n");
head=list1=Create();
printf("list1's Name jbgz\n");
for(p=head;p;p=p->next)
printf("%9s%8d\n",p->name, p->jbgz);
printf("Input list2's name and jbgz:\n");
head=list2=Create();
printf("list2's Name jbdz\n");
for(p=head;p;p=p->next)
printf("%9s%8d\n",p->name, p->jbgz);
head=link(list1,list2);
printf("the all Name jbgz\n");
for(p=head;p;p=p->next)
printf("%9s%8d\n",p->name, p->jbgz);
return 0;
}
struct employee *Create()
{
struct employee *head=NULL,*tail=NULL,*p;
int jbgz;
char name[20];
int size = sizeof(struct employee);
scanf("%s%d",&name,&jbgz);
while(jbgz != 0)
{
p = (struct employee *) malloc(size);
strcpy(p->name, name);
p->jbgz = jbgz;
p->next=NULL;
if(head==NULL)head=tail=p;
else
{
tail->next=p;
tail=p;
}
scanf("%s%d",&name,&jbgz);
}
return head;
}
struct employee*link(struct employee *list1,struct employee *list2)
{
struct employee *p=list2;
while(p->next)
p=p->next;
p->next=list1;
return list2;
}
只要把 list1 的表尾插到 list2 的表头前,再进行一遍遍历即可。