【问题标题】:Inserting into a linked list queue based on a compare function基于比较函数插入链表队列
【发布时间】:2013-11-06 15:48:58
【问题描述】:

我正在实现 QueueADT 的单链表版本。在创建队列时,如果客户端给了我们一个比较函数,我们将使用它来向队列中插入新数据。如果客户端不提供比较功能,我们使用标准的队列插入,只是插入到队列的后面。

我在使用比较函数插入的逻辑上遇到问题。我们只知道比较函数返回什么。

compare( void*a, void*b)
//compare returns < 0 if a < b
//compare returns = 0 if a == b
//compare returns > 0 if a > b

我有你的标准队列和链接节点结构:

typedef struct queueStruct {
    Node *front;
    Node *rear;
    int count;
    int (*compare)(const void*, const void*);
};

typedef struct Node {
    void* value;
    struct Node *next;
}Node;

这是我对插入功能的尝试。我不认为逻辑是正确的,并且希望能得到一些见解,甚至可能是伪代码!

void que_insert(queueStruct queue, void *data){
    //if the queue is empty
    if (que_empty(queue)){
        Node *node;
        node = malloc(sizeof(Node));
        node -> value = data;
        node -> next = NULL;
        queue->front  = node;
        queue->rear = node;
        queue->count++;
     }
     else{
        //if there is no comparison function, just use FIFO
        if (queue->compare == NULL){
            printf("Fifo\n");
            Node *node;
            node = malloc(sizeof(Node));
            node->value = data;
            node->next = NULL;
            queue->rear->next = node;
            queue->rear = node;
            queue->count++;

         }
         else{
            Node *temp;
            temp = queue->front;
            //if what we are adding is smaller than the head, then we found our new head
            if (queue->compare(data, temp->value) < 0){
                printf("Less Than 0\n");
                Node *node;
                node = malloc(sizeof(Node));
                node->value = data;
                node->next = queue->front;
                queue->front = node;
                queue->count++;
                return;
             }
             while (temp->next != NULL){
                if (queue->compare(data, temp->value)> 0){
                    printf("Greater than 0\n");
                    temp = temp->next;
                }
                else if (queue->compare(data, temp->value) ==  0){
                    printf("Equals 0\n");
                    Node *node;
                    node = malloc(sizeof(Node));
                    node->value = data;
                    node->next = temp->next;
                    temp->next = node;
                    queue->count++;
                    return;
                 }
             }
             //temp should be at the rear
             if (queue->compare(data, temp->value)> 0){
                printf("Adding to rear");
                Node *node;
                node = malloc(sizeof(Node));
                node->value = data;
                node->next = NULL;
              }
          }
     }
}

测试:

在尝试将以下数据插入队列时:

42, 17, -12, 9982, 476, 2912, -22, 3291213, 7782

似乎插入这些值直到最后一个,程序挂起

inserting 7782
Greater than 0
Greater than 0
Greater than 0
Greater than 0

【问题讨论】:

  • 嘿,抱歉,我刚刚在这里添加了我正在寻找的内容,谢谢!
  • 但是你已经写了一些代码。那怎么了?
  • 是什么让你得出结论,你的逻辑不正确?那是行不通的?它做什么,您对为什么有任何怀疑吗?您可以做的一件事来清理一些混乱是意识到您总是要添加一个节点,所以将它分配在函数开头的 one 位置,然后函数的其余部分可以专门用于查找将其插入的位置。
  • 不相关:您的Node 结构类型-decl 缺少关键字typedef。它所做的只是声明一个名为Node 的变量。并且queueStruct 缺少类似的声明类型定义,如果它要在您呈现的代码中使用,如图所示。编写时它用作指针类型,但类型定义为结构类型,因此所有 -&gt; 用法甚至都无法编译。
  • 嗨 Craig,添加最后一个值后程序挂起。我编辑了原始帖子以显示这一点。

标签: c linked-list queue


【解决方案1】:

首先。如果您的比较需要严格的弱排序(它应该这样做),则不需要所有添加的比较器调用。这实质上意味着以下

if (compare(a,b) < 0) 
    then a is less than b
else if !(compare(b,a) < 0) 
    then a and b are equal
else b is less than a

这在标准库中很常用,因为一方面它使比较枚举必须更容易理解。它也只需要定义一个逻辑操作。 “少”

正如我在一般评论中所说,合并新节点的分配位置。看来您的队列支持重复(它应该),因此无论您总是添加一个新节点,所以只需从一开始就创建它,然后专注于寻找它的去向。

最后,指向节点的指针将使您的插入更加简洁(事实上,我打赌您会发现它非常短):

void que_insert(queueStruct* queue, void *data)
{
    // going to need this sooner or later
    Node *node = malloc(sizeof(*node));
    node->value = data;
    node->next = NULL;

    //if the queue is empty
    if (queue->count == 0)
    {
        queue->front = queue->rear = node;
        queue->count = 1;
    }

    // else not empty, but no comparator
    else if (queue->compare == NULL)
    {
        //if there is no comparison function, just use FIFO
        queue->rear->next = node;
        queue->rear = node;
        queue->count++;
    }

    // else not empty and we have a comparator
    else
    {   // walk over chain of node pointers until we  find a pointer 
        //  that references a node equal or greater than ours, or EOQ
        Node **pp = &queue->front;
        while (*pp && queue->compare((*pp)->value, data) < 0)
            pp = &(*pp)->next;

        // no more nodes means new rear. otherwise link mid-stream
        if (!*pp)
            queue->rear = node;
        else
            node->next = *pp;

        // either way, this is always done.
        *pp = node;
        queue->count++;
    }
}

工作原理

我们使用指向节点的指针来保存我们正在检查的每个指针的地址,从头指针开始。这有几个优点。我们不需要跟踪指向节点的指针就可以访问它的“下一个”,因为我们已经按地址获得了它。我们得到自动前端插入,因为我们从头指针地址开始,唯一必须考虑的是后端更新,仍然必须手动完成,但是这是微不足道的。

指针到指针的遍历可能看起来有点令人生畏,但它有一些奇妙的特性。没有指向节点的枚举器。您实际上是使用 列表中的指针 作为枚举变量。不仅仅是它们的“值”,还有实际的指针它们的值。您所做的只是更改您正在使用的指针,方法是更新指针指针中的地址以反映 列表中的哪个物理指针(不仅仅是 to您正在处理的列表)。当需要更改某些内容时,您不需要指向需要更改的“下一个”指针的节点的指针;您已经有了要修改的指针的地址,因此可以立即修改。

我没有测试附加的代码,但它应该可以工作。只需确保您的初始queueStruct 设置为零计数,前后指针均为NULL。这(显然)很重要。

最后,我强烈建议使用调试器逐步完成此操作。没有什么可以替代“看到”在代码中飞来飞去的实际指针地址和值。一支铅笔和一张纸,上面有盒子、箭头到盒子、箭头到箭头到盒子也有助于理解算法。

【讨论】:

  • 非常感谢,代码有效!我将听取您的建议并拿起铅笔和纸来帮助我了解这里到底发生了什么。一个问题,当我们没有找到更多节点时,为什么我们不必将旧后部的旁边链接到新后部?
  • @NesBa 您的问题的答案在于如何使用指针对指针的剖析。链表中的最后一个节点有一个next 指针。当我们用尽列表并到达末尾时,地址 pp那个指针持有。该指针将是我们挂新后部的位置。记住。 pp 不指向节点;它指向一个指针。如果它指向的指针为 NULL,这意味着我们在链表末端,因此将新节点挂在该指针上,并将后指针设置为同一个节点。相信我,调试它并在纸上完成它。这将是有道理的。
  • @NesBa 老实说,我认为你会发现头指针的自动魔法更新更有趣,如果节点应该放在 beginning 的列表。这通常会让人们摸不着头脑。我想我无法更好地描述它的工作原理,所以一定要画出来。
猜你喜欢
  • 1970-01-01
  • 2020-10-14
  • 2021-04-28
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
相关资源
最近更新 更多