C++,优先队列的链表

标签 c++ linked-list priority-queue

我正在处理一项任务,目标是制作一个模块,该模块将协助表示显示每个“项目”的优先级队列及其适当的“优先级值”。正如我之前的帖子中所述,我并没有要求任何人为我做“功课”,我只是寻求帮助以了解我的 insertinsertCell< 哪里出了问题 给我错误的函数。任何建议/提示都会对解决我的问题很有帮助。

//Beginning of Code
//File: pqueue.cpp


//Module pqueue.cpp provides priority queues that include items and their 
//priority types. It tests for empty queues, can insert priority queues that
//include a new item with a new priority, can remove priority queues and/or
//items with their priorities, and can print each item along with its priority
//to the standard output.
//
//

#include <cstdio>
#include "pqueue.h"
#include <cstdlib>

using namespace std;

//An object of type PQCell represents a cell in a linked list.
//
//PQCell has three field.
//
//item- the particular item that is entered
//priority- the priority that an item has
//nextList- a pointer to the next cell in the list
struct PQCell
{ 
    ItemType item;
    PriorityType priority;
    PQCell *nextItem;

    PQCell(ItemType a, PriorityType b, PQCell* nextCell)
    {
        item = a;
        priority = b;
        nextItem = nextCell;
    }   
};

//Function isEmpty returns true if the queue is empty and false if it is not.
bool isEmpty(const PriorityQueue& q)
{
    if(q.cells == NULL)
    {
            return false;
    }

    return true;
}   

//Function insertCell inserts item x with priority p into a linked list 'L'.
void insertCell(PQCell*& L, ItemType x, PriorityType p)
{
    if(L==NULL || L -> priority > p)
    {
        L = new PQCell(x, p, L);
    }

    else
    {
        insertCell(L -> nextItem, x,p);
    }
}

//Function insert inserts item x with priority p into a priority queue 'q'.
void insert(PriorityQueue& q, ItemType x, PriorityType p)
{
    insertCell(q, x, p);
}

//Function printPriorityQueue prints a representation of the priority queue 
//including each value, x, and it's priority type, y, respectively.
void printPriorityQueue(const PriorityQueue& q, ItemPrinter printItem, 
        PriorityPrinter printPriority)
{
    PQCell* pointer = q.cells;
    while(pointer != NULL)
    {
        printf("Item = ");
        (printItem)(pointer->item);

        printf("     Priority = ");
        (printPriority)(pointer->priority);

        printf("\n");

        pointer = pointer -> nextItem;
    }
}

//Function remove removes the item with the smallest priority. It also stores
//the item and it's priority into x and p, respectively.
void remove(PriorityQueue& q, ItemType& x, PriorityType& p)
{
    if(q.cells != NULL)
    {
        PQCell *pointer = q.cells;
        q.cells = q.cells -> nextItem;
        x = pointer -> item;
        p = pointer -> priority;
        delete [] pointer;
    }

    else
    {
        printf("Q is empty");
        exit(1);
    }
}

//File: pqueue.h



typedef const char* ItemType;
typedef double      PriorityType;
struct PQCell;
typedef void (*ItemPrinter)(ItemType);
typedef void (*PriorityPrinter)(PriorityType);



struct PriorityQueue
{
    PQCell* cells;

    PriorityQueue()
    {
        cells = NULL;
    }
};

bool isEmpty(const PriorityQueue& q);

void insert(PriorityQueue& q, ItemType x, PriorityType p);

void printPriorityQueue(const PriorityQueue& q, ItemPrinter printItem, 
        PriorityPrinter printPriority);

void remove(PriorityQueue& q, ItemType& x, PriorityType& p);

正如我所说,我遇到的主要问题是 insertinsertCell 函数。我不断收到错误消息:

pqueue.cpp: In function void insert(PriorityQueue*&, ItemType, PriorityType):

pqueue.cpp:70:20: error: invalid initialization of reference of type PQCell*& from expression of type PriorityQueue*
  insertCell(q, x, p);

pqueue.cpp:54:6: error: in passing argument 1 of void insertCell(PQCell*&, ItemType, PriorityType)
 void insertCell(PQCell*& L, ItemType x, PriorityType p)

再一次,任何帮助都会很棒,谢谢。

最佳答案

主要问题(导致 invalid initialization 错误的问题)是您将 PriorityQueue& 传递给期望 PQCell*& 的函数:

//Function insert inserts item x with priority p into a priority queue 'q'.
void insert(PriorityQueue& q, ItemType x, PriorityType p)
{
   // Wrong.
   //insertCell(q, x, p);

   // You probably meant to do this:
   insertCell(q.cells, x, p);
}

我还注意到您的 isEmpty 逻辑似乎是倒退的:

//Function isEmpty returns true if the queue is empty and false if it is not.
bool isEmpty(const PriorityQueue& q)
{
   if(q.cells == NULL)
   {
       return false;
   }

   return true;
}     

如果 q.cells == NULL,您将返回 false,而在这种情况下您可能打算返回 true,并且false 否则。

关于C++,优先队列的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55271464/

相关文章:

c++ - C++ 中的正则表达式如何搜索有效的 Linux 设备节点?

c - C中的链表程序

c++ - 链表类定义

c++ - 谁能解释更多关于优先队列C++中的比较函数

c++ - 如何从 C++ 中的字符串中去除所有非字母数字字符?

android - 使用 CMake 在 C++ android 应用程序中使用 libcurl

c++ - 使用递归反向链表

c++ - 同一类中的多个 Operator() 重载

javascript - 异步优先级队列 - 数字越大优先级越高吗?

c++ - -fno-strict-aliasing 作为函数属性