C 实战练习题目74

题目:连接两个链表。

程序分析:无。

实例:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 struct list
 4 {
 5     int data;
 6     struct list *next;
 7 };
 8 typedef struct list node;
 9 typedef node *link;
10 link delete_node(link pointer,link tmp)
11 {
12     if (tmp==NULL) /*delete first node*/
13         return pointer->next;
14     else
15     {
16         if(tmp->next->next==NULL)/*delete last node*/
17             tmp->next=NULL;
18         else /*delete the other node*/
19             tmp->next=tmp->next->next;
20     return pointer;
21     }
22 }
23 void selection_sort(link pointer,int num)
24 {
25     link tmp,btmp;
26     int i,min;
27     for(i=0;i<num;i++)
28     {
29         tmp=pointer;
30         min=tmp->data;
31         btmp=NULL;
32         while(tmp->next)
33         {
34             if(min>tmp->next->data)
35             {
36                 min=tmp->next->data;
37                 btmp=tmp;
38             }
39             tmp=tmp->next;
40         }
41         printf("\40: %d\n",min);
42         pointer=delete_node(pointer,btmp);
43     }
44 }
45 link create_list(int array[],int num)
46 {
47     link tmp1,tmp2,pointer;
48     int i;
49     pointer=(link)malloc(sizeof(node));
50     pointer->data=array[0];
51     tmp1=pointer;
52     for(i=1;i<num;i++)
53     {
54         tmp2=(link)malloc(sizeof(node));
55         tmp2->next=NULL;
56         tmp2->data=array[i];
57         tmp1->next=tmp2;
58         tmp1=tmp1->next;
59     }
60     return pointer;
61 }
62 link concatenate(link pointer1,link pointer2)
63 {
64     link tmp;
65     tmp=pointer1;
66     while(tmp->next)
67         tmp=tmp->next;
68     tmp->next=pointer2;
69     return pointer1;
70 }
71 int main(void)
72 {
73     int arr1[]={3,12,8,9,11};
74     link ptr;
75     ptr=create_list(arr1,5);
76     selection_sort(ptr,5);
77 }

感谢你的阅读,请用心感悟!希望可以帮到爱学习的你!!分享也是一种快乐!!!请接力。。。

点击查看原文,谢谢!

posted @ 2020-07-14 11:50  C语言自学网  阅读(243)  评论(0编辑  收藏  举报