单链表

//有两个单向链表,头指针分别为list1list2,链表中每一结点信息为姓名、基本工资,请编写程序将单向链表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 的表头前,再进行一遍遍历即可。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值