C++ 将具有优先级的链接移动到列表头的算法
我必须使用一个链表创建一个优先级队列,但我总是遇到分段错误。我首先生成一个没有优先级的队列(优先级=0)。我的算法也适用于排队:C++ 将具有优先级的链接移动到列表头的算法,c++,algorithm,linked-list,queue,priority-queue,C++,Algorithm,Linked List,Queue,Priority Queue,我必须使用一个链表创建一个优先级队列,但我总是遇到分段错误。我首先生成一个没有优先级的队列(优先级=0)。我的算法也适用于排队: 1。创建新链接、第二个链接的指针和交换的指针 2.如果队列为空,则设置newlink=front&back 3.如果没有,检查新链接是否具有任何优先级,如果优先级=0,则移动新链接 排在队伍的后面 4.如果新链接具有任何优先级,请将其移动到前面 5.检查前部是否小于 6.如果第二个链路没有优先级或第一个链路的优先级较低,则不执行交换 7.如果在PriotityQueu
1。创建新链接、第二个链接的指针和交换的指针
2.如果队列为空,则设置newlink=front&back
3.如果没有,检查新链接是否具有任何优先级,如果优先级=0,则移动新链接
排在队伍的后面
4.如果新链接具有任何优先级,请将其移动到前面
5.检查前部是否小于
6.如果第二个链路没有优先级或第一个链路的优先级较低,则不执行交换
7.如果在PriotityQueue::enqueue
函数中第二个链接的优先级为,则您已声明temp=nullptr
。
因此,当您尝试使用temp->data
和temp->priority
交换这两个链接时,您将遇到分段错误
交换可以在没有temp
的情况下完成
void PriorityQueue::enqueue(int val, int priority){
...
else if(front->priority >= linkPtr->priority){ //5.
front->next=linkPtr.next;
linkPtr->next=front;
front=linkPtr;
}
...
}
在列表的第2步中,实际上没有“移动”,而是在列表的末尾或开头添加新节点。至于您的问题,您的问题是什么?您显示的代码有问题吗?您是否只需要一组工作代码?如果你还没有这样做,请花点时间。不,它不起作用,我忘了提到我的算法有一个分段错误。我已经找到了一个更好的方法来做这件事,而不是交换链接,我只是把它插入我需要的地方,哈哈
#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
//System Libraries
#include <iostream>
using namespace std;
class PriorityQueue{
private: struct Link{
int data;
int priority;
Link *next;
};
Link *front, *back;
int listLength;
public: PriorityQueue(){front = nullptr; back = nullptr;};
~PriorityQueue(){destroyList();};
int returnFront();
void printQueue();
void destroyList();
void enqueue(int, int);
void dequeue(int &);
bool isEmpty() const;
};
void PriorityQueue::destroyList(){
int value; // Dummy variable for dequeue
while(!isEmpty())
dequeue(value);
}
void PriorityQueue::enqueue(int val, int priority){
Link *newLink = new Link;//pointer for new link
Link *linkPtr = nullptr; //pointer for second link
Link *temp = nullptr; //pointer for swap of links
//allocating new link to pointer and storing number
newLink->data = val;
newLink->next = nullptr;
newLink->priority = priority;
//If stack is empty, make the new link the top of stack
if(isEmpty()){
front = newLink;
back = newLink;
}
else{
/* 1. move newLink to the front, if priority exists
* 2. make newLink point to the front
* 3. make the newLink the new front of the list
* 4. if 2nd link has no priority or if 1st link has
* less priority,perform no swap
* 5. if 2nd link's priority is <= front link; swap
*/
if(newLink->priority == 0){
back->next = newLink;
back = newLink;
}
else if(newLink->priority >= 1){
newLink->next= front; //1.
linkPtr = newLink->next; //2.
front = newLink; //3.
if(front->priority < linkPtr->priority ||linkPtr->priority == 0){} //4.
else if(front->priority >= linkPtr->priority){ //5.
temp->data = linkPtr->data;
temp->priority = linkPtr->priority;
linkPtr->data = front->data;
linkPtr->priority = front->priority;
front->data = temp->data;
front->priority = temp-> priority;
}
}
}
//track list length
listLength++;
}
//removing links from front
void PriorityQueue::dequeue(int& topVal){
Link *temp = nullptr;
//If Queue is empty, message is displayed after dequeue attempt
if(isEmpty()){
cout<<"Queue is empty."<<endl;
}
else{
//save the top value into num
topVal = front->data;
//delete the front node
temp = front;
front = front->next;
delete temp;
//update list length
listLength--;
}
}
//returns whether the list is empty or not
bool PriorityQueue::isEmpty() const{
bool status;
if(listLength > 0)
status = false;
else
status = true;
return status;
}
//returns the value at the top of the queue
int PriorityQueue::returnFront(){
return front->data;
}
//Displays all values in queue
void PriorityQueue::printQueue(){
Link *newLink = front;
while(newLink != nullptr){
cout<<" "<<newLink->data;
newLink = newLink->next;
}
}
#endif /* PRIORITYQUEUE_H */
void PriorityQueue::enqueue(int val, int priority){
...
else if(front->priority >= linkPtr->priority){ //5.
front->next=linkPtr.next;
linkPtr->next=front;
front=linkPtr;
}
...
}