[英]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.