[C++] 链表 →→→→→进入此内容的聊天室

来自 , 2019-06-13, 写在 C++, 查看 110 次.
URL http://www.code666.cn/view/b8b4b727
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. //node结点
  4. typedef struct node{
  5.         int data;
  6.         struct node *next;
  7. }*LinerList;
  8. //排序法
  9. void paixu(LinerList head,int n)
  10. {
  11.         int i=0,j=0,temp;
  12.         LinerList p2;
  13.         LinerList p1;
  14.  
  15.         for(p1=head;i<n-1;i++,p1=p1->next)
  16.                 for(p2=p1->next;j<n-i-1;j++,p2=p2->next)
  17.                 {
  18.                         if(p1->data>p2->data)
  19.                         {
  20.                                 temp=p2->data;
  21.                                 p2->data=p1->data;
  22.                                 p1->data=temp;
  23.                         }
  24.                 }
  25.  
  26. }
  27. //新建一个链表
  28. void CreateLinerList(LinerList *head,int n)
  29. {
  30.         int data;
  31.         LinerList p,tail;
  32.         printf("请输入链表对应的数据元素:\n");
  33.         scanf("%d",&data);
  34.         p=(LinerList)malloc(sizeof(struct node));
  35.         p->data=data;
  36.         p->next=*head;
  37.         *head=p;
  38.         tail=p;
  39.         while(n>1)
  40.         {
  41.                 scanf("%d",&data);
  42.                 p=(LinerList)malloc(sizeof(struct node));
  43.             p->data=data;
  44.             p->next=NULL;
  45.             tail->next=p;
  46.                 tail=p;
  47.                 n--;
  48. }
  49. }
  50. //显示一个链表
  51. void Show(LinerList p)
  52. {
  53.         while(p!=NULL)
  54.         {
  55.                 printf("%-6d",p->data);
  56.                 p=p->next;
  57.         }
  58.         printf("\n");
  59. }
  60. //归并两个链表
  61. void Combine(LinerList* ha,LinerList hb)
  62. {
  63.         LinerList current,last,other;
  64.     while(hb!=NULL)
  65.         {
  66.                 other=(LinerList)malloc(sizeof(struct node));
  67.                 other->data=hb->data;
  68.                 current=*ha;
  69.                 while(other->data>current->data&&current->next!=NULL)
  70.                 {
  71.                         last=current;
  72.                         current=current->next;
  73.                 }
  74.                 if(other->data<=current->data)
  75.                         if(current==*ha)
  76.                         {
  77.                                 other->next=*ha;
  78.                                 *ha=other;
  79.                                 hb=hb->next;
  80.                         }
  81.                         else
  82.                                 if(other->data==current->data)
  83.                         {
  84.                                 hb=hb->next;
  85.                         }
  86.                          else{
  87.                                 other->next=current;
  88.                                 last->next=other;
  89.                                 hb=hb->next;}
  90.                          else{
  91.                                  other=hb;
  92.                                  current->next=other;
  93.                          }
  94.                          }
  95. }
  96. int main()
  97. {
  98.         LinerList ha=NULL,hb=NULL;
  99.  
  100.         int a,b;
  101.         printf("请输入ha的数据个数a:\n");
  102.         scanf("%d",&a);
  103.         CreateLinerList(&ha,a);
  104.         paixu(ha,a);
  105.  
  106.         printf("请输入hb的数据个数b:\n");
  107.         scanf("%d",&b);
  108.         CreateLinerList(&hb,b);
  109.         paixu(hb,b);
  110.  
  111.         printf("链表ha:");
  112.         Show(ha);
  113.     printf("链表hb:");
  114.         Show(hb);
  115.         printf("\n");
  116.         printf("---------------------------------------------------\n");
  117.         Combine(&ha,hb);
  118.         printf("合并后的链表ha为:");
  119.         Show(ha);
  120.         printf("合并后的链表hb为:");
  121.         Show(hb);
  122. }
  123.  

回复 "链表"

这儿你可以回复上面这条便签

captcha