使用链表C++数组的优先级队列
Priority Queue using array of linked list C++
所以我正在尝试使用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
问题是您在分配内存之前访问 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]->数据的位置。但是该元素尚未分配,因此您会遇到运行时问题。
而不是
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;
}
不应链接的不是数组中的指针。
每个指针都是指向链表(即 10 个独立链表)的 HEAD 的指针。所以不要链接它们。
最后 - 使用 add() 函数中的数组!不要做大的如果陈述。
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
}
顺便说一句 - 记住创建一个析构函数,删除 10 个链表中的所有元素。
相关文章:
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 打印优先级队列
- 带自定义比较器的最小优先级队列
- 优先级队列自定义比较器
- 排序数组优先级队列
- 更改运行时优先级队列的排序功能
- 优先级队列构造函数的工作
- 实现优先级队列
- 优先级队列功能比较
- 在C++中打印对的优先级队列的所有值时出现问题
- 使用堆的优先级队列,具有相同键的值不遵循 FIFO(先进先出)
- 为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?
- 是否可以使用简单队列创建优先级队列
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- Cython中带有自定义比较器的优先级队列
- 优先级队列比较器[C++].
- asio::io_service 具有多个线程的优先级队列处理