链表的快速排序不对
写了一个链表的快速排序,但排了几次之后就不对了,而且会crash,找了半天也不知道什么原因,各位路过的帮忙看一下,不胜感激。
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE
{
int data;
struct NODE * next;
}node;
node * create_link()
{
node * p1 = (node *)malloc(sizeof(node));
node * p2 = (node *)malloc(sizeof(node));
node * p3 = (node *)malloc(sizeof(node));
node * p4 = (node *)malloc(sizeof(node));
node * p5 = (node *)malloc(sizeof(node));
node * p6 = (node *)malloc(sizeof(node));
node * p7 = (node *)malloc(sizeof(node));
node * p8 = (node *)malloc(sizeof(node));
node * p9 = (node *)malloc(sizeof(node));
node * p10 = (node *)malloc(sizeof(node));
p1->data = 5;
p1->next = p2;
p2->data = 3;
p2->next = p3;
p3->data = 6;
p3->next = p4;
p4->data = 1;
p4->next = p5;
p5->data = 2;
p5->next = p6;
p6->data = 8;
p6->next = p7;
p7->data = 7;
p7->next = p8;
p8->data = 10;
p8->next = p9;
p9->data = 9;
p9->next = p10;
p10->data = 4;
p10->next = NULL;
return p1;
}
void swap(node * n1, node * n2)
{
int temp = n1->data;
n1->data = n2->data;
n2->data = temp;
}
node * qsort(node * phead, node * ptail)
{
node * pleft=phead, *plast=phead, *pcur=phead,*pprelast=phead, *temp =NULL, *pre=NULL;
if(phead != NULL && ptail != NULL)
printf("phead is %d , ptail is %d\n",phead->data,ptail->data);
if(phead == NULL || phead->next == NULL || phead == ptail)
return phead;
pcur=phead->next;
plast = phead;
//pre=pprelast=phead;
while(pcur != ptail)
{
if (pcur->data < pleft->data)
{
swap(pcur,plast->next);
pprelast = plast;
plast=plast->next;
}
pcur = pcur->next;
}
if (pcur->data < pleft->data)
{
swap(pcur,plast->next);
pprelast = plast;
plast=plast->next;
}
swap(pleft,plast);
printf("plast is %d , pprelast is %d, plast->next is %d\n",plast->data,pprelast->data,plast->next->data);
temp = phead;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
qsort(pleft,pprelast);
qsort(plast->next,ptail);
return phead;
}
int main(void)
{
node * phead = create_link();
node * temp = phead, *pre=phead;
printf("before sort: ");
while(temp != NULL)
{
printf("%d ",temp->data);
pre=temp;
temp = temp->next;
}
printf("\n");
qsort(phead,pre);
printf("after sort: ");
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE
{
int data;
struct NODE * next;
}node;
node * create_link()
{
node * p1 = (node *)malloc(sizeof(node));
node * p2 = (node *)malloc(sizeof(node));
node * p3 = (node *)malloc(sizeof(node));
node * p4 = (node *)malloc(sizeof(node));
node * p5 = (node *)malloc(sizeof(node));
node * p6 = (node *)malloc(sizeof(node));
node * p7 = (node *)malloc(sizeof(node));
node * p8 = (node *)malloc(sizeof(node));
node * p9 = (node *)malloc(sizeof(node));
node * p10 = (node *)malloc(sizeof(node));
p1->data = 5;
p1->next = p2;
p2->data = 3;
p2->next = p3;
p3->data = 6;
p3->next = p4;
p4->data = 1;
p4->next = p5;
p5->data = 2;
p5->next = p6;
p6->data = 8;
p6->next = p7;
p7->data = 7;
p7->next = p8;
p8->data = 10;
p8->next = p9;
p9->data = 9;
p9->next = p10;
p10->data = 4;
p10->next = NULL;
return p1;
}
void swap(node * n1, node * n2)
{
int temp = n1->data;
n1->data = n2->data;
n2->data = temp;
}
node * qsort(node * phead, node * ptail)
{
node * pleft=phead, *plast=phead, *pcur=phead,*pprelast=phead, *temp =NULL, *pre=NULL;
if(phead != NULL && ptail != NULL)
printf("phead is %d , ptail is %d\n",phead->data,ptail->data);
if(phead == NULL || phead->next == NULL || phead == ptail)
return phead;
pcur=phead->next;
plast = phead;
//pre=pprelast=phead;
while(pcur != ptail)
{
if (pcur->data < pleft->data)
{
swap(pcur,plast->next);
pprelast = plast;
plast=plast->next;
}
pcur = pcur->next;
}
if (pcur->data < pleft->data)
{
swap(pcur,plast->next);
pprelast = plast;
plast=plast->next;
}
swap(pleft,plast);
printf("plast is %d , pprelast is %d, plast->next is %d\n",plast->data,pprelast->data,plast->next->data);
temp = phead;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
qsort(pleft,pprelast);
qsort(plast->next,ptail);
return phead;
}
int main(void)
{
node * phead = create_link();
node * temp = phead, *pre=phead;
printf("before sort: ");
while(temp != NULL)
{
printf("%d ",temp->data);
pre=temp;
temp = temp->next;
}
printf("\n");
qsort(phead,pre);
printf("after sort: ");
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
return 0;
}
...全文
相关推荐
北航软件学院真题参考答案_部分21
因为快速排序法经过一次划分后,1. 不对,a表示数组的第一个位置,不是一个变量;2. str.len求长度,而sizeof求变量在内存中所占据的大小。”
链表排序——归并排序、快速排序
目录 1 归并排序 1.1 实现思路 1.2 代码实现 2 快速排序 2.1 实现思路 2.2 代码实现 1 归并排序 归并排序基本原理https://blog.csdn.net/qq_28114615/article/details/86345717 1.1 实现思路 与数组归并排序相同,主要分为以下三个步骤: ①找到链表中点。最常用的方法是双指针...
青岛大学 2019 921数据结构与算法基础 研究生考试真题
快速排序的长处发挥情况:被排序的数据完全无序的情况下快速排序的长处最易发挥。知识点:快速排序的定义和特点、快速排序的时间复杂度分析。13.
数据结构综合试题期末版及答案
**图**:图由顶点和边组成,邻接矩阵表示图时,对称表示无向图,不对称表示有向图。
Java数据结构和算法
- **冒泡排序**:冒泡排序的基本思想是从数组的第一个元素开始,比较相邻的两个元素,如果顺序不对则交换,重复这个过程直到最后一个元素,整个过程称为一轮冒泡。


