【发布时间】: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<>的原因?) -
看起来有人已经在git-hub 发布了 CS106B 优先队列分配的完整答案。也许它可以帮助?
-
@WhozCraig:好点!我在 dequeueMin 方法的第一个 if 条件下收到 EXC_BAD_ACCESS 错误消息。是的,这是一个练习,目标是处理优先级队列的不同实现。
-
@DavidC.Rankin:非常感谢您提供的链接。我仍在尝试在不查看解决方案的情况下解决它......但这可能会在不久的将来有所帮助;-)
标签: c++ priority-queue doubly-linked-list