【问题标题】:How to use linked list ADT in a function?如何在函数中使用链表 ADT?
【发布时间】:2021-10-23 11:18:29
【问题描述】:

我正在尝试从另一个队列复制一个队列,但该函数给出以下编译错误:

“类节点”没有名为“isEmpty”的成员

“类节点”没有名为“入队”的成员

“类节点”没有名为“getFirst”的成员

“类节点”没有名为“出队”的成员

'不能将'operations*' 转换为'Node*' 作为回报

#include<iostream>
#include "Queue.h"
using namespace std;

Node* copyQueue(Node* q1)
{
    operations *q2 = new operations();

    while(q1->isEmpty()!= True)
    {
        q2 -> enqueue( q1 -> getFirst() );
        q1 -> dequeue();
    }
    return q2;
}

int main()
{
    operations *q1 = new operations();

    int temp, n;
    cout << "Enter queue size: ";
    cin >> n;
    cout << "Enter data to copy: ";
    for( int i = 0 ; i < n ; i++ )
    {
        cin >> temp;
        q1 -> enqueue(temp);
    }
    copyQueue(q1);
    cout << "Copied Queue = ";
    q2 -> display();
}

queue.h 源代码:

#include<iostream>
#include "Node.h"
using namespace std;

class operations
{
public:

    Node *front= NULL;
    Node *newdata= NULL;
    Node *rear= NULL;

    void enqueue(int item)
    {
        newdata = new Node(item);

        if(front==NULL && rear == NULL)
        {
            front = rear = newdata;
        }
        else
        {
            rear->next = newdata;
            rear = newdata;
        }
    }

    void dequeue()
    {
        Node *temp = front;
        front = front->next;
        delete temp;
    }

    bool isEmpty()
    {
        if(front == NULL)
            return true;
        else
            return false;
    }

    int getFirst()
    {
        return front -> item;
    }

    void display()
    {
        Node *temp= front;
        while(temp!= NULL)
        {
            cout<<temp->item<<"->";
            temp= temp->next;
        }
        cout<<endl;
    }
};

【问题讨论】:

  • 还需要看看Node 是如何定义的。拼凑一个minimal reproducible example
  • 猜测一下,copyQueue 的参数类型是错误的。你传给它一个operations *,来自Node 的缺失成员在operations

标签: c++ class linked-list copy queue


【解决方案1】:

您的第一个函数的签名不正确。它应该接受一个队列并返回一个队列,但是您已经为两者指定了Node

所以改变:

Node* copyQueue(Node* q1)

到:

operations* copyQueue(operations* q1)

还有一些需要更正的地方:

  • main 中,您尚未声明q2。此外,它不会捕获来自copyQueue 调用的返回值,因此请更改:

    copyQueue(q1);
    

    到:

    operations *q2 = copyQueue(q1);
    
  • True 应该是true

  • dequeue 应该在队列变空时将rear 设置为NULL。所以改变:

    front = front->next;
    delete temp;
    

    到:

    front = front->next;
    if(front==NULL) 
    {
        rear = NULL;
    }
    delete temp;
    
  • 由于isEmpty() 返回一个布尔值,你不应该写:

    while(q1->isEmpty()!= true)
    

    ...但只是:

    while(!q1->isEmpty())
    
  • 出于同样的原因,isEmpty 方法不需要if...else。变化:

    if(front == NULL)
        return true;
    else
        return false;
    

    到:

    return front == NULL;
    
  • frontNULL 时,rear 必然相同,因此无需再次检查:

    if(front==NULL && rear == NULL)
    

    可以是:

    if(front==NULL)
    

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2014-04-15
    相关资源
    最近更新 更多