首页 > Code > 连接两个链表

连接两个链表

2008年1月4日 timmy 发表评论 阅读评论 558 views


#include
#include
#include
struct worker_struct{
char name[20];
int salary;
struct worker_struct *next;

};
void main()
{
void print_work(struct worker_struct *head);
struct worker_struct *creat();
struct worker_struct *link(struct worker_struct *list1,struct worker_struct *list2);
struct worker_struct *list1,*list2;
list1=creat();
list2=creat();
link(list1,list2);
print_work(list1);
}
struct worker_struct *creat()
{
struct worker_struct *head,*tail,*p;
int salary;
char name[20];
int size=sizeof(struct worker_struct);
head=tail=NULL;
printf(“Input name salary:\n”);
scanf(“%s%d”,name,&salary);
while(salary>=0){
p=(struct worker_struct *)malloc(size);
strcpy(p->name,name);
p->salary=salary;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf(“%20s%6d”,name,&salary);
}
return head;
}
void print_work(struct worker_struct *head)
{
struct worker_struct *ptr;
if(head==NULL){
printf(“\nNO Records\n”);
return;
}
printf(“\nThe Workers' Records Are: \n”);
printf(” Name salary\n”);
for(ptr=head;ptr;ptr=ptr->next)
printf(” %s %6d\n”,ptr->name,ptr->salary);
}
struct worker_struct *link(struct worker_struct *list1,struct worker_struct *list2)
{

struct worker_struct *ptr,*ptr1,*ptr2;
ptr1=list1;
ptr2=list1->next;
ptr=list2;
while(ptr2!=NULL)
{
ptr1=ptr2;
ptr2=ptr1->next;
}
ptr1->next=ptr;
return list1;
}

若日志经rss订阅或导入到外站,可能有些视频和图片无法显示,请点击原文链接查看。
本文链接地址: http://imtimmy.com/%e8%bf%9e%e6%8e%a5%e4%b8%a4%e4%b8%aa%e9%93%be%e8%a1%a8/

转载请注明: 转载自Timmy's Blog

如果你觉得本博内容不错,欢迎 [订阅 Timmy's Blog],以便第一时间了解本博更新内容;


不妨再看看这些相关的日志:

  1. 实验二  抽象数据类型的表示和实现
  2. 实验5__线性表的链式表示和实现
  3. 实验八 队列的基本操作以及舞伴配对问题
  4. 实验十  二叉树的基本操作
  5. 实验5 二叉搜索树的基本操作

分类: Code 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :cool: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O