简体   繁体   English

使用链表实现优先级队列

[英]Implementation of priority queue using linked list

#include<iostream>
using namespace std;

//template<class T>
class Node
{
    public:
//  T data;
    int data;
    Node *next;
    int priority;
};

//template<class T>
class Que
{
        Node *front , *rear;
    public:
        Que(){front = rear = NULL;}

//      void enqueue(T x);
//      T dequeue();
        void enqueue(int *x, int l);
        int dequeue();
        void display();
};

//template<class T>
//void Que<T>::enqueue(T x)
void Que::enqueue(int *x, int l)
{
    Node * pt = front;
    for(int i=0; i<l; i++){
    Node *t = NULL;
    t = new Node;
    if(t==NULL)
        cout<<"Queue is full"<<endl;
    else
    {
        t->next = NULL;
        t->data = x[i];
        t->priority = x[i];
        if(front==NULL)
            front = rear =t;
        else
        {
            if(front->priority <= t->priority)
            {
                t->next = front;
                front = t;
            }
            else
            {
                while(pt->next!= NULL && pt->next->priority <= x[i])
                    pt = pt->next;
                t->next = pt->next;
                pt->next = t;
            }
        }
    }
}
}

//template<class T>
//T Que<T>::dequeue()
int Que::dequeue()
{
//  T x = -1;
    int x = -1;
    Node *t = NULL;
    if(front == NULL)
        cout<<"Queue is empty"<<endl;
    else
    { 
        x = front->data;
        t = front;
        front = front->next;
        delete t;
    }
    return x;
}

//template<class T>
//void Que<T>::display()
void Que::display()
{
    Node *t = front;
    while(t)
    {
        cout<<t->data<<" ";
        t = t->next;
    }
    cout<<endl;
}

int main()
{
//  Que <int> q();
    Que q;
    int a [] = {6, 1, 2, 5, 4, 3};
    q.enqueue(a, sizeof(a)/sizeof(a[0]));
//  q.dequeue();
    q.display();
    return 0;
}

It's a code for priority queue using linked list in C++.这是C++中使用链表的优先级队列代码。 The while loop inside enqueue member function is showing segmentation fault.入队成员 function 内的 while 循环显示分段错误。

I think pointer *pt to node which is used to point front is not pointing correctly.我认为指向用于指向前面的节点的指针*pt没有正确指向。 I have been trying to resolve it but couldn't get any idea.我一直在尝试解决它,但不知道。 What can be reason for it?这可能是什么原因?

You initialize pt at the start of enqueue but never reset it within the loop.您在enqueue开始时初始化pt但从未在循环内重置它。 So when you add multiple elements to an empty list, pt will start as nullptr , and won't be updated after the first element is added to the list.因此,当您将多个元素添加到空列表时, pt将以nullptr开头,并且在将第一个元素添加到列表后不会更新。 When you try to add the second element to the list you dereference pt->next which cause your segmentation fault because pt is still nullptr .当您尝试将第二个元素添加到列表中时,您会取消引用pt->next ,这会导致您的分段错误,因为pt仍然是nullptr

The fix is easy: move Node * pt = front;修复很简单:移动Node * pt = front; to within the for loop.for循环内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM