所以我試圖創建一個優先級隊列使用C++中的鏈接列表數組。我還沒有完成,但如果我能修復構造函數,我想我可以自己完成剩下的工作。 我有一個數據文件,第一行有文件中的項目數。 之後的下一行將有一個字符,然後是從0開始到9的優先級。 因此,我正在對包含26個字母(項目)的字母進行排序。每封信都有一個優先級。例如,問題5(字母Q的優先級爲5) 當我運行它時,它說程序停止工作,然後開始尋找解決方案。就像一個無限循環的錯誤,我想。使用數組鏈表C++的優先級隊列
#include <iostream>
#include <fstream>
using namespace std;
class Queue
{
private:
struct linkedList
{
char data;
linkedList *next;
};
linkedList* PQ[10];
public:
//bool empty;
//bool empty(int priority);
void add(char info, int lvl);
//void remove();
Queue();
};
int main()
{
int size;
char Info;
int Lvl;
Queue Q;
ifstream dataIn;
dataIn.open("charQueueInput.txt");
if (dataIn.fail())
{
cout << "File does not exist." << endl;
exit(1);
}
dataIn >> size;
dataIn.get();
cout << size;
/*for (int i = 0; i < size; i++)
{
dataIn >> Info;
dataIn >> Lvl;
dataIn.get();
Q.add(Info, Lvl);
}*/
system("pause");
return 0;
}
Queue::Queue()
{
for (int i = 0; i < 10; i++)
{
PQ[i] = NULL;
}
for (int i = 0; i < 9; i++)
{
PQ[i]->next = PQ[i + 1];
}
PQ[9]->next = NULL;
}
void Queue::add(char info, int lvl)
{
if (lvl == 0)
{
PQ[0]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[1];
PQ[0]->next = temp;
}
else if (lvl == 1)
{
PQ[1]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[2];
PQ[1]->next = temp;
}
else if (lvl == 2)
{
PQ[2]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[3];
PQ[2]->next = temp;
}
else if (lvl == 3)
{
PQ[3]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[4];
PQ[3]->next = temp;
}
else if (lvl == 4)
{
PQ[4]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[5];
PQ[4]->next = temp;
}
else if (lvl == 5)
{
PQ[5]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[6];
PQ[5]->next = temp;
}
else if (lvl == 6)
{
PQ[6]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[7];
PQ[6]->next = temp;
}
else if (lvl == 7)
{
PQ[7]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[8];
PQ[7]->next = temp;
}
else if (lvl == 8)
{
PQ[8]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[9];
PQ[8]->next = temp;
}
else if (lvl == 9)
{
PQ[9]->data = info;
linkedList *temp = new linkedList;
temp->next = NULL;
PQ[1]->next = temp;
}
}
這裏會是對數據文件的例子:
7
Q 5
W 3
T 0
Y 4
A 9
B 5
U 0
你將它讀成:
0: T -> U
1.
2.
3. W
4. Y
5. Q -> B
6.
7.
8.
9. A
T,U,W,Y,Q,B, A
程序停止工作不是「無限循環」;它是*崩潰*。這意味着你的程序正在調用未定義的行爲。在少量鏈接列表代碼的情況下,*高度*懷疑您正在以某種方式使用僞指針。例如:你的構造函數中有一個用NULL指針填充你的數組的循環,然後在這之後立即執行另一個循環:'PQ [i] - > next = PQ [i + 1]'I.e你解除引用NULL。 – WhozCraig