链表建立,排序--自认为比较完美,哈哈,有点自恋
/****************************
* 时间:2008-4-18
* 功能:链表建立,排序
*****************************/
#include <stdio.h>
#include <malloc.h>
//声明节点类型
typedef struct node
{
int value;
struct node *next;
}*List,node;
//------------------------
void CreateList(List &Li)
{
List L=(List)malloc(sizeof(node));
L->next=NULL;
Li=L;
for(int i=10;i>0;i--)
{
node * newnode=(node *)malloc(sizeof(node));
printf("第%d \t",10-i);
scanf("%d",&newnode->value);
newnode->next=L->next;
L->next=newnode;
}
}
//---------------------------
void Output(List L)
{
while(L->next!=NULL)
{
L=L->next;
printf("%d ",L->value);
}
}
//---------------------------
void Compositor(List &L)
{
List temp;
List h=L;
for(int i=1;i<=10;i++)
{
//在循环体内要注意,每一次需要把H还是指向头节点
h=L;
for(int j=10;j-i>=0&&h->next->next!=NULL;j--)
{
//这里的j要注意每一次外部循环一次,内部循环的次数少一,特别注意需要j--
if(h->next->value>h->next->next->value)
{
temp=h->next;
h->next=h->next->next;
h=h->next;
temp->next=h->next;
h->next=temp;
}
else
h=h->next;//晕啊,如果if条件不满足,一定不能少了h=h->next
}
}
}
void main()
{
List L;
CreateList(L);
Output(L);
Compositor(L);
printf("\n");
Output(L);
}
评论
发表评论