cdsn_Python 2023-03-06 15:33 采纳率: 69%
浏览 31
已结题

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


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++)
    {
//        printf("%d %d",i,n);
        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;
            }
            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
希望能帮忙解释一下

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

  • 写回答

5条回答 默认 最新

  • CSDN专家-link 2023-03-06 15:41
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 3月14日
  • 已采纳回答 3月6日
  • 创建了问题 3月6日

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)