【问题标题】:Unsorted doubly linked list for priority queue of strings in C++C++中字符串优先级队列的未排序双向链表
【发布时间】:2014-08-02 04:51:13
【问题描述】:

我正在尝试使用未排序的双向链表实现字符串的“优先级”队列,但我完全停留在 dequeue 方法上(在代码的底部/之前可能存在问题)。优先级队列是指第一个要出队的元素是最小元素的队列。

您介意看看我的代码,并给我一些提示,说明我哪里错了吗?

非常感谢您的帮助。

马特

/*************************************************************
 * File: pqueue-doublylinkedlist.cpp
 *
 * Implementation file for the DoublyLinkedListPriorityQueue
 * class.
 */

#include "pqueue-doublylinkedlist.h"
#include "error.h"

/* Implementation notes: DoublyLinkedListPriorityQueue constructor
 * ----------------------------------------------------------------
 * This function initializes an empty priority queue represented as a doubly 
 * linked-list.
 */

DoublyLinkedListPriorityQueue::DoublyLinkedListPriorityQueue() {
    listHead = new Cell;
    listHead = NULL;
    count = 0;
}

/* Implementation notes: DoublyLinkedListPriorityQueue destructor
 * --------------------------------------------------------------
 * This function deletes every cell in the priority queue.
 */

DoublyLinkedListPriorityQueue::~DoublyLinkedListPriorityQueue() {
    Cell *temp, *link;
    temp = link = new Cell;

    temp = listHead;
    while (temp != NULL) {
        link = temp->next;
        delete temp;
        temp = link;
    }
    count = 0;
}

/* Implementation notes: DoublyLinkedListPriorityQueue size
 * --------------------------------------------------------
 * Returns the size of the priority queue.
 */

int DoublyLinkedListPriorityQueue::size() {
    return count;
}

/* Implementation notes: DoublyLinkedListPriorityQueue isEmpty
 * -----------------------------------------------------------
 * Returns true if there is no cell within the list.
 */

bool DoublyLinkedListPriorityQueue::isEmpty() {
    return (count == 0);
}

/* Implementation notes: DoublyLinkedListPriorityQueue enqueue
 * -----------------------------------------------------------
 * Enqueues the new Cell into the chain just after the head Cell.
 */

void DoublyLinkedListPriorityQueue::enqueue(string value) {

    Cell *newOne = new Cell;
    newOne->str = value;
    newOne->prev = NULL;

    newOne->next = listHead;
    listHead = newOne;

    count++;
}

/* Implementation notes: DoublyLinkedListPriorityQueue peek
 * --------------------------------------------------------
 * Returns the string value of the next node to be dequeued.
 */

string DoublyLinkedListPriorityQueue::peek() {
    if (isEmpty()) error("peek an empty list");

    curr = new Cell;

    curr = listHead;
    string result = listHead->str;

    for (curr = listHead; curr != NULL; curr = curr->next) {
        if (curr->str != "" && curr->str < result) {
            result = curr->str;
        }
    }

    return result;
}

/* Implementation notes: DoublyLinkedListPriorityQueue dequeueMin
 * --------------------------------------------------------------
 * Deletes the node with the smallest string and returns this string value.
 */

string DoublyLinkedListPriorityQueue::dequeueMin() {
    if (isEmpty()) error("dequeueMin an empty list");

    Cell *temp;
    temp = curr = new Cell;
    temp = curr = NULL;

    string result = listHead->str;

    // find the node to delete and store a pointer on it
    for (curr = listHead; curr != NULL; curr = curr->next) {
        if (curr->str != "" && curr->str < result) {
            result = curr->str;
            temp = curr;
        }
    }

    // first position (if: node to delete prev == NULL)
    if (temp->prev == NULL) {
        temp = listHead->next;
        delete listHead;
        listHead = temp;

    // delete from last position (else if: node to delete next == NULL)
    } else if (temp->next == NULL) {
        curr = temp->prev;
        curr->next = NULL;
        delete temp;

    // other position (else)
    } else {
        temp->prev->next = temp->next;
        temp->next->prev = temp->prev;
        delete temp;
    }

    count--;

    return result;
}

【问题讨论】:

  • 如果您详细阐述一个问题和一个相关的问题,它可能会极大地增加您提出意见的机会。它是一个优先级队列,因此输出期望可能是可推断的,但是 you 正在经历什么*,它有什么不同? I-think-something-is-wrong 有点喊出这个问题,“是什么让你这么想?” (除此之外,这是一个练习,还是有其他不应该使用 std::priority_queue&lt;&gt; 的原因?)
  • 看起来有人已经在git-hub 发布了 CS106B 优先队列分配的完整答案。也许它可以帮助?
  • @WhozCraig:好点!我在 dequeueMin 方法的第一个 if 条件下收到 EXC_BAD_ACCESS 错误消息。是的,这是一个练习,目标是处理优先级队列的不同实现。
  • @DavidC.Rankin:非常感谢您提供的链接。我仍在尝试在不查看解决方案的情况下解决它......但这可能会在不久的将来有所帮助;-)

标签: c++ priority-queue doubly-linked-list


【解决方案1】:

将优先级队列实现为双向链表(或者实际上,实现一个)是非常不寻常的,因为有一个 STL 实现可供使用:

#include <iostream>
#include <queue>
#include <functional>

int main(void) {
    std::priority_queue<std::string, std::vector<std::string>,
            std::greater<std::string> > pq;

    pq.push("foo");
    pq.push("bar");
    pq.push("quux");

    while( !pq.empty() ) {
        std::cout << pq.top() << std::endl;
        pq.pop();
    }

    return 0;
}

我发布此内容的假设是,您可能根本不知道此功能可用,而不是有真正的原因您想要自己实现并以相当奇怪的方式进行方式。

如果我错了,并且上面的指针对你没有用,我建议你将你的实现调整为标准版本的接口(尽可能多)并解释你为什么不想使用STL 版本,因为这些步骤将增加 StackOverflow 用户(可能比我回答您的问题的人,以及未来有类似问题的用户)对您的代码的熟悉度。

【讨论】:

  • 感谢您的留言。我应该准确地说这是斯坦福的作业(CS106B),其目标是让学生以 4 种不同的方式实现优先级队列。我做了向量实现,订购了单链表,但我卡在了双链表上。还是谢谢!
  • @matt1101:他们在夏季赛跑 106B?
  • 我认为他们有,但我不在斯坦福。我正在从 2013 年春季开始学习编程。指针对我来说有点挑战。
  • 你是对的,他们确实在夏季运行。我很惊讶。一开始,指针对每个人来说都是一个挑战,但是一旦它们“点击”,它们就会成为第二天性,你将无法理解为什么你会遇到它们的问题。指针实际上只是对象(在松散意义上)存储在内存中的内存地址以及该对象的大小(以便指针算术起作用)。如果您知道如何使用双向链表创建、维护和导航最大堆结构,那么您就完成了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多