关于链表冒泡排序的问题,如何解决?


struct node
{
    int number;
    struct node* nextPtr;
};
int main()
{
    
    struct node* head=(struct node*)malloc(sizeof(struct node*));
    int n,cnt=0;
    scanf("%d",&n);
    struct node* buf=head;
    while (n!=-1)
    {
        struct node* temp=(struct node*)malloc(sizeof(struct node*));
        temp->number=n;
        temp->nextPtr=NULL;
        buf->nextPtr=temp;
        buf=buf->nextPtr;
        scanf("%d",&n);
        cnt+=1;
    }
    struct node*p,*q,*tail,*temp;
    n=cnt;
    
    for (int i=0;i1;i++)
    {
//        printf("%d %d",i,n);
        q = head->nextPtr;
        p = q->nextPtr;
        tail=head;
        for (int j=0;j1-i;j++)
        {
            
            if (q->number>p->number);
            {
                q->nextPtr = p->nextPtr;
                p->nextPtr = q;
                tail->nextPtr = p;
            }
            tail = tail->nextPtr;
            q = tail->nextPtr;
            p = q->nextPtr; 
        }
    }
    temp=head->nextPtr;
    while (temp!=NULL)
    {
        if (temp->nextPtr==NULL)
        {
            printf("%d",temp->number);
        }
        else
        {
            printf("%d ",temp->number);
        }
        temp=temp->nextPtr;
        
    }
    
}

做c语言链表冒泡排序问题,请问为什么我运行完数列还不是递增数列
例如输入2 1 4 3 -1
然后输出3 4 2 1
希望能帮忙解释一下

找了网上的一些标答感觉没什么区别。

36行if语句后面的分号删掉

在条件语句中多写了一个分号,导致条件判断的结果始终为真,从而导致排序错误。应该将if (q->number>p->number);改为if (q->number>p->number)。另外在交换两个节点的位置时,没有同时更新它们的前驱节点的指针,修改后:

img


#include <stdio.h>
#include <stdlib.h>
struct node {
    int number;
    struct node* nextPtr;
};
int main() {
    struct node* head = (struct node*)malloc(sizeof(struct node));
    int n, cnt = 0;
    scanf("%d", &n);
    struct node* buf = head;
    while (n != -1) {
        struct node* temp = (struct node*)malloc(sizeof(struct node));
        temp->number = n;
        temp->nextPtr = NULL;
        buf->nextPtr = temp;
        buf = buf->nextPtr;
        scanf("%d", &n);
        cnt += 1;
    }
    struct node *p, *q, *tail, *temp;
    n = cnt;
    for (int i = 0; i < n - 1; i++) {
        q = head->nextPtr;
        p = q->nextPtr;
        tail = head;
        for (int j = 0; j < n - 1 - i; j++) {
            if (q->number > p->number) {
                q->nextPtr = p->nextPtr;
                p->nextPtr = q;
                tail->nextPtr = p;
                q = p;
                p = tail->nextPtr->nextPtr;
            }
            tail = tail->nextPtr;
            q = tail->nextPtr;
            p = q->nextPtr;
        }
    }
    temp = head->nextPtr;
    while (temp != NULL) {
        if (temp->nextPtr == NULL) {
            printf("%d ", temp->number);
        } else {
            printf("%d ", temp->number);
        }
        temp = temp->nextPtr;
    }
    return 0;
}

你没理解什么叫冒泡
都已经改链表了,形式上还是照搬数组
q = head->nextPtr;怎么能写在循环里面呢,q永远是头节点,你到底谁跟谁比较啊
你不要过度关注i,j两重循环的形式
你要搞清楚冒泡到底是谁在跟谁比较,搞清楚了可以改成双重while循环来代替
-=-=-=
此外,36行多了个分号

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632