繁体   English   中英

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

[英]Unsorted doubly linked list for priority queue of strings in C++

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

您介意查看我的代码,并给我一些提示我哪里出错的地方吗?

非常感谢你的帮助。

马特

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM