gpt4 book ai didi

c++ - 使用链表C++数组的优先级队列

转载 作者:行者123 更新时间:2023-11-28 06:18:42 24 4
gpt4 key购买 nike

<分区>

因此,我尝试使用 C++ 中的链表数组创建优先级队列。我还没有完成,但如果我能修复构造函数,我想我可以自己完成剩下的工作。我有一个数据文件,第一行是文件中的项目数。此后的下一行将有一个字符,然后是从 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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com