【发布时间】:2009-12-02 18:56:21
【问题描述】:
我想创建一个从类 List 继承的链表类 ListList。 ListList 使用 List 中的函数,但有自己的函数。它有自己的指向列表开头的起始指针,以及拥有不同数量元素的 Node 结构。
但是,看起来,当从 ListList 调用 List 的函数之一时,List 使用它自己的起始指针和节点。但我希望使用 ListList 的起始指针和节点。 有人可以帮我解决这个问题吗? 我可以发布一些代码,但我不知道哪一部分是相关的......
这是我上面所说的列表
class LinkList
{
public:
LinkList(); //constructor that sets the start pointer to NULL, to show that the list is empty
~LinkList(); //destructor that deletes each node in the linked list
LinkList(const LinkList &original); //copy constructor
void addToken(string token); //creates a node with the given token and places it at the beginning of the linked list
string showList(); //returns a string of tokens, separated by commas and spaces
bool findToken(string token); //searches linked list for the given token, returns true if the token is in the list
string getToken(string word); //searches linked list for a token that begins with the given word.
//Returns the full token if there's a token that begins with the given word, else returns an empty string
void deleteList();
protected:
struct Node //each node of the linked list, held together by the next pointer
{
string token;
bool second_word; //tells whether or not there is a space within the token (a two-word keyword)
//This could be easily changed to an int that tells how many words are within the keyword (for multi-word keywords)
Node *next; //pointer to the next node of the linked list. NULL if there is no next node
};
Node *start; //pointer to the beginning of the linked list, and the last added node
bool twoWordToken(string token); //returns true if there is a space located within a token, meaning the token consists of two words.
};
这是我上面叫ListList的那个
class LinkListList: public LinkList
{
public:
LinkListList(); //modified contructor initiates the pointers start and ptrNode
~LinkListList(); //modified destructor deletes all nodes and secondaryList nodes
LinkListList(const LinkListList &original); //copy constructor
bool addSubList(LinkList subList, string commandWord); //calls setPtrNode, then adds the given subList to that node
bool findSubToken(string commandWord, string token); //calls setPtrNode, then calls on that node's secondaryList's findToken function
//returns true if the findToken function returns true, else returns false
string showSubList(string commandWord); //returns a string of tokens, separated by commas and spaces, representing the subList of the given token
string getSubToken(string word, string commandWord); //searches commandWord's subList for a token that begins with the given word.
//Returns the full token if there's a token that begins with the given word, else returns an empty string
private:
struct Node //each node of the linked list, held together by the next pointer
{
string token;
bool second_word; //tells whether or not there is a space within the token (a two-word keyword)
LinkList secondaryList; //keeps a list of all related words
Node *next;
};
Node *start; //pointer to the beginning of the linked list
Node *ptrNode; //this pointer is used for the functions
void setPtrNode(string token); //sets ptrNode to point to the node containing the specified token. ptrNode is NULL if the token could not be found
};
【问题讨论】:
-
最有用的示例代码可能是定义 List 和 ListList 的头文件。
-
你需要发布类定义
-
您不只是使用 STL 列表有什么原因吗?
标签: c++ inheritance linked-list