C ++中字符串优先级队列的未排序双链表

时间:2014-08-02 04:51:13

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

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

你介意看看我的代码并给我一些关于我错在哪里的提示吗?

非常感谢你的帮助。

亚光

/*************************************************************
 * 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;
}

1 个答案:

答案 0 :(得分:0)

将优先级队列实现为双向链接列表(或者实际上实现一个)非常不寻常,因为可以使用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用户的代码的熟悉程度(那些可能比我更好地回答您的问题的人以及具有类似问题的未来用户)。