#include<stdio.h>
#include<malloc.h>
struct Node
{
struct Node *next;
int value;
};
struct Node* InsertSort(void)
{
struct Node *Head=NULL,*New,*Cur,*Pre;
int i;
for(i=0;i<5;i++)
{
if((New=(struct Node*)malloc(sizeof(struct Node)))==NULL)
{
printf("申请空间失败/n");
return NULL;
}
printf("请输入节点的value/n");
scanf("%d",&New->value);
New->next=NULL;
if(Head==NULL)
{
Head=New;
continue;
}
if(New->value<=Head->value)
{//head之前插入节点
New->next=Head;
Head=New;
continue;
}
Cur=Head;
while(New->value>Cur->value && Cur->next!=NULL)
{
Pre=Cur;
Cur=Cur->next;
}
if(Cur->value>=New->value)
{
Pre->next=New;
New->next=Cur;
}
else
//if(Cur!=New)
Cur->next=New;
}//for
return Head;
}
void print(struct Node* Head)
{
struct Node* Cur;
Cur=Head;
while(Cur!=NULL)
{
printf("Cur->value=%d/n",Cur->value);
Cur=Cur->next;
}
}
void main(void)
{
struct Node* head;
head=InsertSort();
print(head);
}