我曾尝试为我的链表程序构建一个排序算法,该算法将通过检查第一个元素与第二个、第三个等,然后将第二个元素与其余元素等进行比较,将我的链表按字母顺序排序,但它不是工作。怎么了?
struct list* sort_list(struct list *head)
{
struct list *current= (struct list*)malloc(sizeof(struct list));
struct list *previous=(struct list*)malloc(sizeof(struct list));
struct list *point=(struct list*)malloc(sizeof(struct list));
char tmp[30];
current = head;
previous = NULL;
point=head;
while(point!=NULL)
{
while (current != NULL)
{
if(strcmp(point->data,current->data)>0)
{
swap(head,point,current);
}
previous = current;
current = current->next;
}
point=point->next;
}
return head;
}
void swap(struct list *head,struct list *first,struct list *second) {
char *tempValue;
tempValue = first->data;
first->data = second->data;
second->data = tempValue;
}
列表结构:
struct list
{
char *data;
struct list *next;
}list;