两个链表的连接

重新开始写代码。先从数据结构开始:两上链表的链接(来自经典100例的改进)

  1 #include <stdlib.h>
  2 #include <stdio.h>
  3 
  4  // #define NUM 5
  5 
  6  struct list
  7 {
  8      int data;
  9      struct list *next;
 10 };
 11 
 12  // typedef struct list node;
 13  // typedef struct node *link;
 14 
 15 typedef  struct list node;
 16 
 17  // typedef  node *link;                                // 这样也行的。
 18  typedef  struct list *link;
 19 
 20  // 删除一个结点
 21  link delete_node(link pointer,link tmp)
 22 {
 23     link del;
 24      if(tmp==NULL) // 删除第一个
 25      {
 26         del = tmp;
 27         free(del);
 28          return pointer->next;
 29     }
 30      // return pointer;
 31 
 32      else
 33     {
 34          if(tmp->next->next==NULL) // delete the last;
 35          {
 36             free(tmp->next);
 37             tmp->next = NULL;
 38         }
 39          else // delete other
 40          {
 41             del=tmp->next;
 42              // free(del);
 43              tmp->next = tmp->next->next;
 44             free(del);
 45         }
 46          return pointer;
 47     }
 48 
 49 }
 50 
 51  // 从小到大输出并且删除
 52  void selection_sort(link pointer, int num)
 53 {
 54     link tmp,btmp;
 55      int i,min;
 56      for(i= 0;i<num;i++)
 57     {
 58         tmp=pointer;
 59         min=tmp->data;
 60         btmp=NULL;
 61          while(tmp->next)
 62         {
 63              if(min>tmp->next->data) // 这里可以访问到最后的元素。
 64              {
 65                 min=tmp->next->data;
 66                 btmp = tmp;
 67             }
 68             tmp = tmp->next;
 69         }
 70 
 71          /*     if(min>tmp->data)//应该加的链尾的判断。
 72              {
 73              min = tmp->data;
 74              btmp = tmp;
 75              } */
 76         printf( " \40%d ",min);
 77         pointer=delete_node(pointer,btmp);
 78     }
 79     printf( " \n ");
 80 }
 81 
 82  // 创建链表
 83  link create_list( int array[], int num)
 84 {
 85     link tmp1,tmp2,pointer;
 86      int i;
 87     pointer =(link)malloc( sizeof(node));
 88     pointer->data = array[ 0];
 89     tmp1 = pointer;
 90      for(i= 1;i<num;i++)
 91     {
 92         tmp2 = (link)malloc( sizeof(node));
 93         tmp2 -> next = NULL;
 94         tmp2->data = array[i];
 95         tmp1->next = tmp2;
 96         tmp1=tmp1->next;
 97          // tmp2->next = NULL;
 98      }
 99 
100      return pointer;
101 
102 }
103 
104  //   连接两个链表。
105  link concatenate(link pointer1,link pointer2)
106 {
107     link temp = pointer1;
108 
109      while(temp->next!=NULL)
110         temp=temp->next;
111 
112     temp->next = pointer2;
113      return pointer1;
114 }
115 
116  int main()
117 {
118      int arr1[]={ 3, 12, 8, 9, 11};
119      int arr2[]={ 5, 4, 2, 1};
120     link ptr1,ptr2;
121 
122      // 建立两个链表。
123      ptr1 = create_list(arr1, 5);
124      // selection_sort(ptr1,5);
125      ptr2 = create_list(arr2, 4);
126 
127      // 连接
128      concatenate(ptr1,ptr2);
129     selection_sort(ptr1, 9);
130 
131      return  0;
132 }

 

 

转载于:https://www.cnblogs.com/zhengmian/archive/2012/04/27/link.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值