Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 由csv文件填充的结构化列表中的气泡排序_C++_Linked List_Bubble Sort - Fatal编程技术网

C++ 由csv文件填充的结构化列表中的气泡排序

C++ 由csv文件填充的结构化列表中的气泡排序,c++,linked-list,bubble-sort,C++,Linked List,Bubble Sort,我加载一个填充结构的.csv文件 typedef struct list TList; struct list { int index; char data; TList* prox; }; 如何在列表中进行气泡排序 我试了一下 void bubble(TList *list, int siz) { int c = 0; int x, y, temp; for (x = (siz - 1); x >

我加载一个填充结构的.csv文件

typedef struct list TList;
    struct list {
        int index;
        char data;
        TList* prox;
    };
如何在列表中进行气泡排序

我试了一下

void bubble(TList *list, int siz) {
    int c = 0;
    int x, y, temp;

    for (x = (siz - 1); x >= 0; x--) {
        c++;
        for (y = 1; y <= x; y++) {
            c++;
            if (list->index[y - 1] > list->index[y]) {
                temp = list->index[y - 1];
                list->index[y - 1] = list->index[y];
                list->index[y] = temp;
            }
        }
    }
    printf("\nNeeded Steps: %i\n", c);
}
void bubble(TList*list,int siz){
int c=0;
整数x,y,温度;
对于(x=(siz-1);x>=0;x--){
C++;
对于(y=1;y索引[y-1]>列表->索引[y]){
临时=列表->索引[y-1];
列表->索引[y-1]=列表->索引[y];
列表->索引[y]=temp;
}
}
}
printf(“\n需要的步骤:%i\n”,c);
}

我认为这是因为
列表->索引[y]
。它就像数据库中的一个表。。。在位置0(索引),我有数据和指向下一个节点的指针。使用
list->index[…]
我想传递位置并获得该节点,就像数组一样。它是一个链表,
prox
需要指向下一个节点。我用一个.csv文件中的数据填充了我的列表,并且
列表->prox
指向下一个节点。

您的内部if不正确(if条件以及交换)。 您希望交换整个TList,而不仅仅是每个TList的索引。例如,如果您有两个这样的TList:

TList[0]       TList[1]
Index1         Index0
"Data for 1"   "Data for 0"  
交换后,您需要

TList[0]       TList[1]
Index0         Index1
"Data for 0"   "Data for 1"
相反,您的内部循环执行以下操作:

TList[0]       TList[1]
Index0         Index1
"Data for 1"   "Data for 0"

但实际上它并没有做到这一点,因为你的if条件并不像mooingduck在评论中指出的那样有效。提示:您不想更改任何TLists中的任何内容,只想更改它们彼此的相对位置。

STL提供了很好的内置排序算法。检查

这将允许您在链表上使用类似于数组的索引运算符

        if ((*list)[y - 1] > (*list)[y]) {

为什么,为什么你想用气泡排序?这是一个算法的经典例子,基本上总是错误的选择

如果您是在真实代码中执行此操作,只需使用标准集合(
std::list
如果您坚持使用链表,如果您头脑清醒,则可能使用
std::vector
),然后使用
std::sort
进行排序

如果你这样做是为了做作业,并且必须使用你自己的列表结构和糟糕的算法,那么你应该使用指针而不是下标来实现你的迭代和比较,所以你的比较就像

if (pos->index > pos->next->index)
    /* swap items */
您(可能)还希望至少将内部循环基于指针操作,而不是整数和索引——其增量操作可能类似于
pos=pos->next


还注意到,使用单链表,很难进行涉及当前节点的交换,因此,您经常要考虑下一个节点和之后的那个节点(至少在可以的时候——即,不处理第一个节点)。

<代码>列表>索引[…] /代码>毫无意义。代码>索引是一个
int
,而不是数组。你想在这里做什么?当
index
被声明为
int
时,
index[y-1]
意味着什么?不……我想这是因为list->index[y]。发生了什么?你以为会发生什么?这是一个自滚链表吗?这是C++还是C(对我来说像C)?请在这个问题中尽可能多地提供信息。它就像数据库中的一个表……在位置0(索引),我有数据,还有指针指向下一个RealStestYes,这是C++标准库的一部分,但是我怎么才能通过一个列表呢?如果你想做的就是在文件中排序数字。解析出数字并将其存储在向量中,在向量上调用STL排序。我想你可以得到你想要的。包括我必须做什么来实现这一点?
if (pos->index > pos->next->index)
    /* swap items */