有2个问题
1.你用错算法了,你的这个算法没办法完成排序
参考这个,你应该能明白你的算法无法完成排序#include <stdio.h>
#include <stdlib.h>
struct Node
{
int a;
struct Node *next;
};
void getInput(struct Node *number)
{
scanf("%d", &number->a);
}
void addlibrary(struct Node **library)
{
struct Node *number;
static struct Node *tail;
number = malloc(sizeof(struct Node));
if(!number)
{
printf("对不起内存分配失败!\n");
exit(1);
}
getInput(number);
if(*library != NULL)
{
tail->next = number;
number->next = NULL;
}
else
{
*library = number;
number->next = NULL;
}
tail = number;
}
void sort(struct Node *library)
{
struct Node *current = library;
while(current)
{
struct Node *min = current;
struct Node *p = current->next;
while(p)
{
if(min->a > p->a)
min = p;
p = p->next;
}
int temp = current->a;
current->a = min->a;
min->a = temp;
current = current->next;
}
}
void printflibrary(struct Node *library)
{
struct Node *temp = library;
while(temp)
{
printf("%d ", temp->a);
temp = temp->next;
}
printf("\n");
}
int main()
{
struct Node *library = NULL;
int ch = 10;
while(ch--)
addlibrary(&library);
sort(library);
printflibrary(library);
return 0;
}
/*
9 1 0 2 8 3 7 4 6 5
0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
*/
第2个问题
要修改一级指针,你需要二级指针
参考这个代码#include <stdio.h>
#include <stdlib.h>
struct Node
{
int a;
struct Node *next;
};
void getInput(struct Node *number)
{
scanf("%d", &number->a);
}
void addlibrary(struct Node **library)
{
struct Node *number;
static struct Node *tail;
number = malloc(sizeof(struct Node));
if(!number)
{
printf("对不起内存分配失败!\n");
exit(1);
}
getInput(number);
if(*library != NULL)
{
tail->next = number;
number->next = NULL;
}
else
{
*library = number;
number->next = NULL;
}
tail = number;
}
void sort(struct Node **library)
{
struct Node *current = *library;
while(current->next)
{
struct Node *min = current;
struct Node *p = current->next;
while(p->next)
{
if(min->next->a > p->next->a)
min = p;
p = p->next;
}
struct Node *temp = current->next;
current->next = min->next;
min->next = temp;
temp = min->next->next;
min->next->next = current->next->next;
current->next->next = temp;
current = current->next;
}
current = *library;
struct Node *p = current;
while(p->next)
{
if(current->a > p->next->a)
p = p->next;
else
break;
}
if(p == current && current->a < current->next->a)
return;
*library = current->next;
current->next = p->next;
p->next = current;
}
void printflibrary(struct Node *library)
{
struct Node *temp = library;
while(temp)
{
printf("%d ", temp->a);
temp = temp->next;
}
printf("\n");
}
int main()
{
struct Node *library = NULL;
int ch = 10;
while(ch--)
addlibrary(&library);
sort(&library);
printflibrary(library);
return 0;
}
/*
8 1 9 0 2 7 3 6 4 5
0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
*/
void sort(struct Node **library)
sort的参数改成了二级指针,因为要修改main函数中的library变量,library是一级指针,所以这里的参数要二级指针,这样才能修改main函数中的那个library
sort(&library);
在main函数中这样使用sort函数
如果链表的第0个元素不存储数据,那么通过交换指针的方式排序是可行的
但是如果链表的第0个元素也存储数据,那么就不适合通过交换指针的方式排序
我不知道这个代码你能不能看懂
因为链表的第0个元素也存储数据,这个数据结构不适合通过交换指针的方式排序,非要这样做也不是不可以,就是像我这样,通过算法来适应数据结构,就导致了我把代码写成这样,很不好理解,说真的,我理解这个代码都够呛,但是我还是勉强能理解,毕竟这个代码是我写的
^_^ |