#include <stdlib.h>
#include <stdio.h>
//#define NUM 5
struct list
{
int data;
struct list *next;
};
//typedef struct list node;
//typedef struct node *link;
typedef struct list node;
//typedef node *link; //这样也行的。
typedef struct list *link;
//删除一个结点
link delete_node(link pointer,link tmp)
{
link del;
if(tmp==NULL)//删除第一个
{
del = tmp;
free(del);
return pointer->next;
}
//return pointer;
else
{
if(tmp->next->next==NULL)//delete the last;
{
free(tmp->next);
tmp->next = NULL;
}
else//delete other
{
del=tmp->next;
//free(del);
tmp->next = tmp->next->next;
free(del);
}
return pointer;
}
}
//从小到大输出并且删除
void selection_sort(link pointer,int num)
{
link tmp,btmp;
int i,min;
for(i=0;i<num;i++)
{
tmp=pointer;
min=tmp->data;
btmp=NULL;
while(tmp->next)
{
if(min>tmp->next->data)//这里可以访问到最后的元素。
{
min=tmp->next->data;
btmp = tmp;
}
tmp = tmp->next;
}
/* if(min>tmp->data)//应该加的链尾的判断。
{
min = tmp->data;
btmp = tmp;
}*/
printf("\40%d",min);
pointer=delete_node(pointer,btmp);
}
printf("\n");
}
//创建链表
link create_list(int array[],int num)
{
link tmp1,tmp2,pointer;
int i;
pointer =(link)malloc(sizeof(node));
pointer->data = array[0];
tmp1 = pointer;
for(i=1;i<num;i++)
{
tmp2 = (link)malloc(sizeof(node));
tmp2 -> next = NULL;
tmp2->data = array[i];
tmp1->next = tmp2;
tmp1=tmp1->next;
//tmp2->next = NULL;
}
return pointer;
}
// 连接两个链表。
link concatenate(link pointer1,link pointer2)
{
link temp = pointer1;
while(temp->next!=NULL)
temp=temp->next;
temp->next = pointer2;
return pointer1;
}
int main()
{
int arr1[]={3,12,8,9,11};
int arr2[]={5,4,2,1};
link ptr1,ptr2;
//建立两个链表。
ptr1 = create_list(arr1,5);
//selection_sort(ptr1,5);
ptr2 = create_list(arr2,4);
//连接
concatenate(ptr1,ptr2);
selection_sort(ptr1,9);
return 0;
}
来源:https://www.cnblogs.com/zhengmian/archive/2012/04/26/2471897.html