无法获取指向双向链表中另一个节点的节点指针

Cannot get a node pointer to point to another node in a doubly linked list

我正在从事一个项目,该项目要求我模拟一个客户列表,我可以在一行的前面和后面添加和删除这些客户。为此,我选择使用 doubly-linked 列表数据结构,并创建了两个 类、Client 和 ClientLine 来完成它。我的 类 的 header 如下所示。

    class Client
{
public:

//Default constructor.
//This constructor automatically fills the QueryTime private member
//with a random value upon creation.
Client();

//***** setQueryTime *****
//A function to set the QueryTime private member to a specific value.
void setQueryTime(float SetTime);

//***** getQueryTime *****
//A function to get the private member, QueryTime.
float getQueryTime();

private:

//A variable to hold the time it will take the secretary to answer
//the question.
float QueryTime;

}; //class Client


class ClientLine
{
public:

//A node to go into the list of clients.
typedef struct{

    struct clientNode *next; //Points to the next node in the list
    struct clientNode *prev; //Points to the previous node in the list
    Client currentClient; //The client in the current spot in line.
    int clientType; //Tells whether the client is in person or on the phone.

} clientNode;

//Default constructor
//Creates an empty doubly-linked list to represent a
//line of clients (phone and in person).
ClientLine();

//***** isLineEmpty *****
//Boolean function to tell if there is nobody in line.
//The line is empty if listHeader->front == listHeader->rear == NULL.
bool isLineEmpty();

//***** addClientRear *****
//Adds a new client to the rear of the line.
void addClientRear(Client newClient, int clientType);

//***** addClientFront *****
//Adds a new client to the front of the line.
void addClientFront(Client newClient, int clientType);

//***** removeClientRear *****
//Deletes the node at the rear of the list and returns the client from it.
Client removeClientRear();

//***** removeClientFront *****
//Deletes the node at the front of the list and returns the client from it.
Client removeClientFront();

private:

clientNode *front; //Pointer to the front of the list
clientNode *rear; //Pointer to the rear of the list
int listLength; //A variable to hold the length of the list.

}; //class ClientLine

在我的 addClientRear 成员函数中,我尝试使用以下代码根据需要将新节点中的指针指向列表中的前一个节点:

//***** addClientRear *****
//Adds a new client to the rear of the line.
void ClientLine::addClientRear(Client newClient, int clientType){
    //Make a new node to contain the client.
    clientNode *newNode = new clientNode;
    newNode->currentClient = newClient;
    newNode->clientType = clientType;

    //Set the pointers in the new node and list as needed.
    if(isLineEmpty()){
        front = newNode;
        rear = newNode;
        newNode->next = NULL;
        newNode->prev = NULL;
    }

    else{
        newNode->prev = rear;
        newNode->next = NULL;
    }

    listLength += 1; //increase the length of the list by one.
} //addClientRear()

但是我在 newNode->prev = rear 语句中收到错误消息:

a value of type "ClientLine::clientNode *" cannot be assigned to an entity of type "clientNode *"

我不确定为什么会出现此错误,因为我正在尝试将一个指针值分配给另一个指针。任何建议将不胜感激。

您有两个不同的 clientNode 结构声明(如错误消息所示)。以 C++ 方式而不是 C 方式定义结构。

struct clientNode {
    clientNode *next; //Points to the next node in the list
    clientNode *prev; //Points to the previous node in the list
    Client currentClient; //The client in the current spot in line.
    int clientType; //Tells whether the client is in person or on the phone.
};

如果不清楚,这里是你的版本,有一些评论

typedef struct{

    struct clientNode *next; // this forward declares clientNode
    struct clientNode *prev;
    Client currentClient;
    int clientType;

} clientNode; // this defines ClientLine::clientNode

这两个声明是不同的。