gpt4 book ai didi

c++ - 返回节点指针的函数

转载 作者:行者123 更新时间:2023-11-28 00:23:58 25 4
gpt4 key购买 nike

各位!我正在制作自己的链表模板以供练习和将来使用;但是,我的其中一个功能遇到了问题:

节点* LinkedList::FindNode(int x);//意味着遍历列表并返回指向包含 x 的指针作为其数据。

当尝试在我的实现文件中声明它时,我不断收到 Node 未定义和不兼容错误的消息。

这是我的头文件:

#pragma once
using namespace std;

class LinkedList
{
private:
struct Node
{
int data;
Node* next = NULL;
Node* prev = NULL;
};

//may need to typedef struct Node Node; in some compilers

Node* head; //points to first node
Node* tail; //points to last node
int nodeCount; //counts how many nodes in the list

public:
LinkedList(); //constructor
~LinkedList(); //destructor
void AddToFront(int x); //adds node to the beginning of list
void AddToEnd(int x); //adds node to the end of the list
void AddSorted(int x); //adds node in a sorted order specified by user
void RemoveFromFront(); //remove node from front of list; removes head
void RemoveFromEnd(); //remove node from end of list; removes tail
void RemoveSorted(int x); //searches for a node with data == x and removes it from list
bool IsInList(int x); //returns true if node with (data == x) exists in list
Node* FindNode(int x); //returns pointer to node with (data == x) if it exists in list
void PrintNodes(); //traverses through all nodes and prints their data
};

如果有人能帮我定义一个返回节点指针的函数,我将不胜感激!

谢谢!

最佳答案

由于 Node 是在另一个类中声明的,您是否记得在实现中引用它时包含类名?

LinkedList::Node *LinkedList::FindNode(int x) { ... }

在类声明中不需要前缀,因为声明在类内部,因此 Node 隐式可用。

关于c++ - 返回节点指针的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26012025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com