我正在研究优先队列的代码,我们的教授要求我们使用单链表。我已经在这方面工作了大约一个星期,而且每次都还没有得到任何正常工作的东西。我查看了类,头文件,模板文件和main中的所有函数,但没有看错。我试图找出问题所在,每次我改变某些问题时,无论改变在哪里,问题都保持不变。代码如下所示。 UnsortedPQ.tpp
#include "CustomExceptions.h"
using namespace std;
template <typename Type>
UnsortedPQ<Type>::UnsortedPQ(){
qSize = 0;
}
template <typename Type>
UnsortedPQ<Type>::UnsortedPQ(Type *dataArray, int n){
qSize = 0;
for(int i = 0; i < n; i++){
insertItem(dataArray[i]);
}
}
template <typename Type>
UnsortedPQ<Type>::~UnsortedPQ(){
}
template <typename Type>
bool UnsortedPQ<Type>::isEmpty(){
if(pQueue.head == NULL){
return true;
}
return false;
}
template <typename Type>
int UnsortedPQ<Type>::size(){
return qSize;
}
template <typename Type>
void UnsortedPQ<Type>::insertItem(Type data){
pQueue.push(data);
qSize++;
}
template<typename Type>
Type UnsortedPQ<Type>::removeMin(){
if(isEmpty() == true){
throw EmptyQueueExcept;
}
else{
qSize = qSize - 1;
return pQueue.findMinimum(true, 0);
}
}
template<typename Type>
Type UnsortedPQ<Type>::minValue(){
if(isEmpty() == true){
throw EmptyQueueExcept;
}
else{
return pQueue.findMinimum(false, 0);
}
}
UnsortedPQ.h
#ifndef UNSORTEDPQ_H
#define UNSORTEDPQ_H
#include <exception>
#include "CustomExceptions.h"
#include "List.tpp"
using namespace std;
template <typename Type>
class UnsortedPQ{
private:
int qSize;
List<Type> pQueue;
public:
UnsortedPQ(void);
UnsortedPQ(Type *dataArray, int n);
~UnsortedPQ(void);
bool isEmpty(void);
int size(void);
void insertItem(Type data);
Type removeMin(void);
Type minValue(void);
};
#include "UnsortedPQ.tpp"
#endif
LinkedList.tpp
#ifndef LIST_TPP
#define LIST_TPP
#include <cstdio>
#include <cstdlib>
using namespace std;
template <typename Type>
class List{
public:
//Node structure to create the links for the list
struct Node{
Type data;
Node* next;
};
//Head pointer to represent the start of a List
Node* head;
//Singly-Linked List constructor
List(){
head = NULL;
}
//Destructor
~List(){
while(head!= NULL){
Node* temp = head->next;
delete head;
head = temp;
}
}
//The method to add a new node and create the data entry for it
void push(Type e){
if(head == NULL){
head = new Node;
head->data = e;
head->next = NULL;
}
else{
Node* temp = new Node;
temp->data = e;
temp->next = head;
head = temp;
}
}
//Removes a node in the list and sticthes the list back together
void removeNode(Node* node){
if(node->next != NULL && node != NULL){
Node* temp = node->next;
node->data = temp->data;
node->next = temp->next;
node = temp;
}
else{
delete node;
}
}
//Looks for the minimum value based on whether it is the sorted or unsorted PQ, then decides whether to delete it or not
//0 is an unsorted, 1 is sorted
Type findMinimum(bool remNode, int stylePQ){
Type minimumValue;
Node* minimum = head;
Node* check = head;
if(stylePQ == 0){
while(check != NULL){
if((check->data) < (minimum->data)){
minimum = check;
}
check = check->next;
}
minimumValue = minimum->data;
if(remNode == true){
removeNode(minimum);
}
}
else if(stylePQ == 1){
minimumValue = minimum->data;
if(remNode == true){
removeNode(minimum);
}
}
return minimumValue;
}
};
#endif
唯一有效的数字集是在降序集上调用removeMin()函数n-1次。因此,例如{10,9,8,7,6}将返回6,7,8,9,但尝试调用10会导致nullpointer错误。使用随机顺序将在一定次数后导致错误,具体取决于订单的排序方式。我无法弄清楚是否是导致此问题的删除功能,或者它是否与构建列表的方式相符。