所以我尝试使用C ++中的链表列表创建优先级队列。我还没完成,但如果我可以修复构造函数,我想我可以自己完成其余的工作。 我有一个数据文件,第一行有文件中的项目数。 之后的下一行将有一个char,然后是从0到9的优先级。 所以我要对包含26个字母(项目)的字母表进行排序。每封信都有一个优先级。 防爆。 Q 5(字母Q优先级为5) 当我运行它时,它表示程序停止工作,然后它开始寻找解决方案。就像我认为无限循环的错误一样。
#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
答案 0 :(得分:1)
问题是您在分配内存之前访问PQ。
class Queue
{
private:
struct linkedList
{
char data;
linkedList *next;
};
linkedList* PQ[10]; // Allocates pointer only
该类仅分配指向linkenList但不指定任何实例的指针。
稍后你有:
// In constructor
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[i] is NULL so run time error
}
以及稍后
void Queue::add(char info, int lvl)
{
if (lvl == 0)
{
PQ[0]->data = info; // Access to non-allocated element!
linkedList *temp = new linkedList;
temp->next = PQ[1];
您可以访问PQ [0] - &gt;数据。但是元素尚未分配,因此您遇到了运行时问题。
而不是
if (lvl == 0)
{
PQ[0]->data = info;
linkedList *temp = new linkedList;
temp->next = PQ[1];
PQ[0]->next = temp;
}
你需要这样的东西:
if (lvl == 0)
{
if (PQ[0] == nullptr)
{
PQ[0] = new linkedList; // If head of queue is null, allocate
// new element for head
PQ[0]->next = nullptr;
}
linkedList *temp2 = PQ[0];
while(temp2->next != nullptr) // Search the linked list
{ // to find last element
temp2 = temp2->next;
}
// Now temp2 points to the last element in the list
temp2->next = new linkedList; // Allocate new element and add it
// to the list after temp2
// to get a linked list
temp2 = temp2->next; // Make temp2 point to the new element
temp2->next = nullptr; // Remember to initialize next of new element
temp2->data = info; // Save info
}
在构造函数中:
Queue::Queue()
{
for (int i = 0; i < 10; i++)
{
PQ[i] = nullptr; // Use nullptr instead of NULL
}
// Remove this block
// for (int i = 0; i < 9; i++)
// {
// PQ[i]->next = PQ[i + 1];
// }
// PQ[9]->next = NULL;
}
数组中的指针不是应链接的。
每个指针是指向链表的HEAD的指针,即10个独立的链表。所以不要把它们联系起来。
最后 - 在add()函数中使用数组!不要做大的if语句。
void Queue::add(char info, int lvl) // Tip: Consider making lvl an unsigned int
{
if ((lvl >= 10) || (lvl < 0)) return; // Ignore if lvl is out of range
if (PQ[lvl] == nullptr) // <---- USE lvl to index array
{
PQ[lvl] = new linkedList; // If head of queue is null, allocate
// new element for head
PQ[lvl]->next = nullptr;
}
linkedList *temp2 = PQ[lvl];
while(temp2->next != nullptr) // Search the linked list
{ // to find last element
temp2 = temp2->next;
}
// Now temp2 points to the last element in the list
temp2->next = new linkedList; // Allocate new element and add it
// to the list after temp2
// to get a linked list
temp2 = temp2->next; // Make temp2 point to the new element
temp2->next = nullptr; // Remember to initialize next of new element
temp2->data = info; // Save info
}
BTW - 记得创建一个析构函数,删除10个链表中的所有元素。