#include <stdio.h>
#include <stdlib.h>
static int count = 0;//计数
typedef struct node
{
int data;
struct node *next;
}Node;
Node * createList()
{
Node*head = (Node *)malloc(sizeof(Node));
head->next = NULL;
int data;
printf("输入数据输入0结束:");
scanf("%d",&data);
while(data)
{
count++;
Node*cur = (Node*)malloc(sizeof(Node));
cur->data = data;
cur->next = head->next;
head->next = cur;
scanf("%d",&data);
}
return head;
}
void sortList(Node* head, int len)
{
for(int i = 0;i<len-1;i++)
{
Node *cur = head->next;
for(int j = 0;j<len-1-i;j++)
{
Node *next = cur->next;
if(cur->data>next->data)
{
int tmp = cur->data;
cur->data = next->data;
next->data = tmp;
}
cur = cur->next;
}
}
}
void traversal(Node *head)
{
head = head->next;
while(head != NULL)
{
printf("%d ",head->data);
head = head->next;
}
}
int main(void)
{
Node *head= createList();//头插法
sortList(head,count);//排序
traversal(head);//遍历
free(head);//释放
return 0;
}