我有一個涉及不同鏈表操作的任務。其中之一涉及重載方括號運算符以便能夠打印鏈表的第i個元素。我做了所有其他事情,但我真的迷失了。這是我正在與之合作。 List類如下:C++重載[]打印第n個鏈表的列表
class List {
public:
// Creates a default empty list
List();
// Simple destructor
~List();
// Insert "data" at the very end of the list
void AddToFront(int data);
// Remove and return the first data item from the list.
int deleteFront();
// Prints the list
void Print() ;
// Returns the size of the list
unsigned int Size() const;
//overloaded assignment operator
Node operator[](unsigned int i) ;
private:
Node *m_head;
};
而且,這裏是我的節點類:
class Node {
public:
Node();
~Node();
Node(int data);
int m_data;
Node *m_next;
};
在重載[]運算符任何幫助將不勝感激。
你會如何編寫一個正常的函數來返回第i個節點? – immibis