【问题标题】:C++, Linked Lists for Priority QueuesC++,优先级队列的链表
【发布时间】:2019-03-20 23:12:12
【问题描述】:

我正在处理一项任务,目标是制作一个模块,以帮助表示优先级队列,显示每个“项目”及其适当的“优先级值”。我没有要求任何人为我做我之前帖子中所述的“功课”,我只是请求帮助了解我的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)

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

【问题讨论】:

  • PriorityQueue&amp; 不是PQCell*&amp;。这清楚地解释了所有错误消息。从我所见,该调用应该将q.cells 作为第一个参数传递。
  • 与其传递指向insertCell()的指针的引用,不如直接返回指向新的PQCell的指针?我发现引用和指针的混合令人困惑,您是否有理由不使用指针到指针?我们不再教指针对指针了吗?
  • 我将代码更改为您的建议,并修复了@WhozCraig 的错误,谢谢。
  • 很公平 - 但Param *&amp; 在我工作过的任何公司都不会通过代码审查。
  • @Kingsley 你是说Param** 会通过代码审查吗?对我来说似乎更糟。

标签: c++ linked-list priority-queue


【解决方案1】:

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

//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

【讨论】:

  • 感谢您指出 isEmpty 中的错误。是的,q.cells 是正确的做法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
相关资源
最近更新 更多