#include
#include
#include
struct number //链表节点
{
int num;
struct number *next;
};
int n;
struct number *creat() //创建链表
{
struct number *head; //头节点
struct number *p1, *p2;
n = 0;
p1 = p2 = (struct number*)malloc(sizeof(struct number));
scanf("%d", &p1->num);
head = NULL;
while (p1->num != 0) //循环输入链表数据,当输入为0时结束,链表数据不包括0
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct number*)malloc(sizeof(struct number));
scanf("%d", &p1->num);
}
p2->next = NULL;
return (head);
}
void print(struct number *head) //链表从小到大排序,并输出
{
struct number *p1, *p2, *p;
int i, j, t;
printf("这%d个数从小到大的排序为:\n", n);
if (head != NULL)
{
//冒泡排序
for (j = 0; j < n - 1; j++)
{
p1 = head; p2 = head;
for (i = 0; i < n - 1 - j; i++)
{
p2 = p1->next;
if (p1->num >= p2->num)
{
t = p1->num;
p1->num = p2->num;
p2->num = t;
}
p1 = p1->next;
}
}
}
p = head;
if (head != NULL)
{
//输出链表值
do
{
printf("%3d", p->num);
p = p->next;
} while (p != NULL);
}
}
void main()
{
struct number *head;
head = creat();
print(head);
}
同意楼上 改改格式再打出来吧
这代码看完能把人累死