# 排序(线性表)

（线性表）已知不带头结点的线性链表node，链表中结点构造为（data、next），其中data为数据域，next为指针域。请写一算法，将该链表按结点数据域的值的大小从小到大重新链接。

#include <stdio.h>
#include <stdlib.h>  //对应malloc函数
struct Node
{
int data;
struct Node *next;
};
struct Node *creat(struct Node *head)   //尾插建表
{
int n,i;
struct Node *current=NULL,*previous=NULL;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
current=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&current->data);
current->next=NULL;
if(i==1)
else
previous->next=current;
previous=current;
}
}
struct Node *sort(struct Node *head)    //插入排序
{
struct Node *unsorted,*previous,*current,*sorted,*inserted;
return NULL;
while(unsorted!=NULL)  //还有未排序结点
{
inserted=unsorted;   //当前插入结点
unsorted=unsorted->next;

current=sorted;
while((current!=NULL)&&(current->data<inserted->data))
{
previous=current;
current=current->next;
}
if(current==sorted)    //插入到链表的头部
sorted=inserted;
else
/**************/

添加代码

/**************/

}
return sorted;
}
{
struct Node *p;
{
}
}
int main()
{
{
}
return 0;
}

6 8 9 10 22

``````7
11 3 8 9 44 26 55``````

``3 8 9 11 26 44 55 ``

``````#include"stdio.h"
#include"stdlib.h"
int n,i;
struct node{
int date;
struct node *next;
};
{
struct node *p1=NULL,*p2=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p1=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p1->date);
p1->next=NULL;
if(i==1)
else
p2->next=p1;
p2=p1;
}
}
{
struct node *first,*p,*q,*t;
while(first!=NULL)
{
first=first->next;
else
p->next=t;
t->next=q;
}
}

int main(){
while(temp)
{
printf("%d ",temp->date);
temp=temp->next;

}
return 0;
}

``````

``````#include"stdio.h"
#include"stdlib.h"
int n,i;
struct node{
int date;
struct node *next;
};
{
struct node *p1=NULL,*p2=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p1=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p1->date);
p1->next=NULL;
if(i==1)
else
p2->next=p1;
p2=p1;
}
}
{
struct node *first,*p,*q,*t;
while(first!=NULL)
{
first=first->next;
else
p->next=t;
t->next=q;
}
}

int main(){
while(temp)
{
printf("%d ",temp->date);
temp=temp->next;

}
return 0;
}

``````