在由 csv 文件填充的结构化列表中进行冒泡排序

发布于 2024-12-19 02:32:42 字数 922 浏览 5 评论 0原文

我加载了一个填充我的结构的 .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);
}

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

I load a .csv file that fill my struct

typedef struct list TList;
    struct list {
        int index;
        char data;
        TList* prox;
    };

How can I do a bubble sort in my list ?

I tried the follow

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);
}

I think this is because the list->index[y]. It's like a table in a database... at position 0 (index), I have the data, and the pointer to the next node. With list->index[..] I want to pass the position and get that node, like an array. It is a linked-list and prox needs to point to the next node. I filled my list with the data that came from a .csv file, and the list->prox points to the next node.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最近可好 2024-12-26 02:32:42

STL 提供了很好的内置排序算法。检查 http://www.cplusplus.com/reference/algorithm/

STL provides nice built-in sort algorithm. check http://www.cplusplus.com/reference/algorithm/

红墙和绿瓦 2024-12-26 02:32:42
typedef struct list TList;
struct list {
    int index;
    char data;
    TList* prox;

    //give the list an [] like an array
    char& operator[](int index) {
        TList* cur = this;
        while(cur->index != index) {
            if (cur->prox == NULL)
                throw std::exception("INVALID INDEX");
            cur = cur->prox;
        }
        return cur->data;
    }
};

这将使您可以像数组一样在链接列表上使用索引运算符。

        if ((*list)[y - 1] > (*list)[y]) {
typedef struct list TList;
struct list {
    int index;
    char data;
    TList* prox;

    //give the list an [] like an array
    char& operator[](int index) {
        TList* cur = this;
        while(cur->index != index) {
            if (cur->prox == NULL)
                throw std::exception("INVALID INDEX");
            cur = cur->prox;
        }
        return cur->data;
    }
};

This will let you use the index operator on your linked list kinda like an array.

        if ((*list)[y - 1] > (*list)[y]) {
怕倦 2024-12-26 02:32:42

您的内部 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 条件不起作用,正如 Mooing Duck 在评论中指出的那样。提示:您不想更改任何 TList 中的任何内容,您只想更改它们之间的相对位置。

Your inner if is incorrect (both the if conditional as well as the the swap).
You want to swap the entire TList, not just the indexes of each TList. For example, if you have two TLists like this:

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

after swapping, you want

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

Instead, your inner loop does this:

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

But it doesnt do this actually, because your if conditional doesn't work as Mooing Duck pointed out in the comments. Hint: You don't want to change anything in any of the TLists, you just want to change where they are relative to each other.

儭儭莪哋寶赑 2024-12-26 02:32:42

为什么哦为什么你想使用冒泡排序?这是算法的经典示例,但本质上总是是错误的选择!

如果您在实际代码中执行此操作,只需使用标准集合(如果您坚持使用链接列表,则可以使用 std::list ,如果您坚持使用链表,则可能使用 std::vector )保持理智),然后使用 std::sort 进行排序。

如果您正在为诸如家庭作业之类的事情执行此操作,并且确实必须使用自己的列表结构和可怕的算法,那么您将使用指针而不是下标来实现迭代和比较,因此您的比较将类似于

if (pos->index > pos->next->index)
    /* swap items */

您将(可能)还希望至少将内部循环基于指针操作而不是整数和索引 - 它的增量操作可能类似于 pos=pos->next 。

另请注意,对于单链表,很难进行涉及当前节点的交换,因此您经常需要考虑下一个节点及其后的节点(至少当您可以时 - 即,不处理第一个节点)。

Why oh why would you want to use a bubble sort? It's the classic example of an algorithm that's essentially always the wrong choice!

If you're doing this in real code, just use a standard collection (std::list if you insist on a linked list, probably std::vector if you're being sane), and then use std::sort to do the sorting.

If you're doing this for something like homework, and really must use your own list structure and a horrible algorithm, then you'd implement your iteration and comparison using pointers instead of subscripts, so your comparison would be something like

if (pos->index > pos->next->index)
    /* swap items */

You'll (probably) also want to base at least your inner loop on pointer operations instead of integers and indexing -- its increment operation will probably be something like pos=pos->next.

Also note that with a singly linked list, it's difficult to do a swap involving the current node, so you frequently want to consider the next node and the one after that (at least when you can -- I.e., not dealing with the first node).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文