首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在双向链表上使用冒泡排序

在双向链表上使用冒泡排序
EN

Stack Overflow用户
提问于 2015-09-11 02:01:45
回答 1查看 420关注 0票数 2

这是我的双向链表实现和它的冒泡排序。其他函数运行良好,但打印函数在我对列表进行冒泡排序后没有给出任何输出。

代码语言:javascript
运行
复制
//
//  Double_linked_list.c
//
//
//  Created by Dengke Liu on 9/7/15.
//
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list.h"


// initialize the list structure;
void init(slist_t *list){
    list->head=NULL;
    list->tail=NULL;
}

// add a string to the end of the list
void append(slist_t *list, char *val){

    // creating a newItem object
    list_item_t* newItem = (list_item_t*)malloc(sizeof(list_item_t));
    newItem->value = val;
    newItem->next=NULL;

    // if there are no elements, just use newItem as head
    if (!list->head) {
        newItem->prev=NULL;
        list->head=newItem;
        list->tail=newItem;
    }else{
        // otherwise append the node and point the tail at the end
        newItem->prev=list->tail;
        list->tail->next=newItem;
        list->tail=newItem;
    }
}

// print the elements of the list
void print(slist_t *list){

    list_item_t* temp=list->head;
    while (temp!=NULL) {
        printf("%s\n", temp->value);
        temp=temp->next;
    }
}

// empty the list
void empty(slist_t *list){

    list_item_t *temp=list->head;
    while (temp->next!=NULL) {
        temp=temp->next;
        free(temp);
    }
    free(list->head);
}

// sort the elements in list in lexical order using bubble sort
void bubblesort(slist_t *list){

    if (list->head==NULL) {
        printf("this is an empty list");
    }

    // to record the comparision state
    bool swapped=true;
    list_item_t *temp;

    while (swapped) {

        swapped=false;
        temp=list->head;

        // iterate through the list to swap unordered elements
        while (temp!=list->tail) {
            // compare two elements, if they are disordered, then swap
            if(strcmp(temp->value, temp->next->value)>0){

                // swap the elements
                if (temp->prev!=NULL) {
                    temp->prev->next=temp->next;
                }
                if (temp->next->next!=NULL) {
                    temp->next->next->prev=temp;
                }

                temp->next=temp->next->next;
                temp->next->next=temp->prev;
                temp->next->next=temp;
                temp->prev=temp->next;

                // change the swap record
                swapped=true;
            }
            temp=temp->next;
        }

        print(list);
    }
}

int main(){
    slist_t *list;
    init(list);
    append(list, "blue");
    append(list, "yellow");
    append(list, "black");
    append(list, "red");
    append(list, "green");
    print(list);
    bubblesort(list);
    print(list);
    empty(list);
    return 0;
}

main()中的第一个print函数给出了正确的输出,而第二个print函数没有给出任何输出。有人能帮我调试一下吗?

EN

回答 1

Stack Overflow用户

发布于 2015-09-11 04:16:18

你的程序大体上看起来是合理的,但我越看越发现细节上的错误。特别地,

1)在函数main()中,您声明了变量list,一个指针,但从未初始化它。然后将这个不确定的值传递给init()函数,该函数继续取消对它的引用,就好像它是一个有效的指针一样。这会产生未定义的行为。您可以为list动态分配存储空间,但在这种情况下,这样做会更容易:

代码语言:javascript
运行
复制
int main() {
    slist_t my_list;
    slist_t *list = &my_list;
    /* ... */

2)当您的bubblesort()函数执行涉及所指向的节点的交换时,它无法更新list->headlist->tail。这将在排序过程中和之后都会导致问题。

3)您的bubblesort()函数没有正确交换列表节点。有不止一种方法可以做到这一点,但你实际实现的并不是其中之一。它首先在temp->next->next=temp->prev处中断,因为此时temp->next已经更新,结果是temp->next->next不是您想要修改的指针之一。构造这种交换的一种更简单的方法是从列表中删除节点temp,然后重新插入一个位置。仔细跟踪哪个指针指向什么。绘制一张图表可能会有所帮助。

4)您的bubblesort()函数应该避免在执行交换的内部循环迭代期间设置temp = temp->next。在这些情况下,您不知道temp与新的temp->next相比如何,甚至不知道是否还有temp->next。如果没有(例如,如果新的temp->nextNULL),那么更新temp将是灾难性的。如果不发生这种情况,完成一次排序例程就很幸运了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32508748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档