我已经编写了两种打印链表数据的方法。一个是正常工作的印刷品。一个用于以相反的顺序打印列表。我想让我的print方法接受reversePrint
并像往常一样完成剩下的工作。由于操作员超载,我无法理解如何使其工作。我的方法如下。请忽略void * cast并使用template <class T>
std::ostream& operator <<(std::ostream& str, LinkedList<T> const& data){
data.print(str);
return str;
}
template <class T>
void LinkedList<T>::print(std::ostream& str) const{
Node *curr = head;
while(curr != nullptr){
str << curr->data << "\n";
curr = curr->next;
}
}
template <class T>
void* LinkedList<T>::printReverse(std::ostream& str) {
Node* curr = head;
//Node* last = (Node*)getLastNode(head);
Node* rHead = nullptr;
while(curr != nullptr){
Node* temp = curr->next;
curr->next = rHead;
rHead = curr;
curr = temp;
}
while(rHead != nullptr){
std::cout << rHead->data << "\n";
rHead = rHead->next;
}
return (void*)rHead;
}
方法返回。
template <class T>
class LinkedList {
private:
struct Node {
T data;
Node *next;
Node(T data, Node *next) :
data(data), next(next) {
}
};
Node* head;
public:
LinkedList() :
head(nullptr) {
}
~LinkedList() {
Node *temp;
for (; head; head = temp) {
temp = head->next;
std::cout << "Destructor called for " << temp->data;
delete head;
}
}
void append(T item);
void insert_at_head(T value);
void* getLastNode(Node* n);
void print(std::ostream& str = std::cout) const;
void* printReverse(std::ostream& str = std::cout);
};
类定义。
{{1}}
答案 0 :(得分:1)
printReverse
可能不修改列表。
我会递归地写它。想法:
template <class T> void LinkedList<T>::printReverse(std::ostream &str, Node const* head) {
if (!head) return;
printReverse(str, head->next);
std::cout << head->data << "\n";
}
例如课堂上的声明:
void printReverse(std::ostream &str) const { printReverse(str, head); }
private:
void static printReverse(std::ostream &str, Node const* head);
偏离主题:
<强> Live On Coliru 强>
#include <iostream>
template <class T> class LinkedList {
private:
struct Node {
T data;
Node *next;
//Node(T data, Node *next) : data(data), next(next) {}
};
Node *head;
public:
LinkedList() : head(nullptr) {}
~LinkedList() {
while (head) {
Node* temp = head->next;
std::cout << "Destructor called for " << head->data;
delete head;
head = temp;
}
}
void append(T item) {
Node* t = getLastNode(head);
(t?t->next : head) = new Node { item, nullptr };
}
void insert_at_head(T value);
Node const*getLastNode(Node const*n) const {
for (Node const* it = n; it; it = it->next)
if (!it->next) return it;
return nullptr;
}
Node*getLastNode(Node*n) {
for (Node* it = n; it; it = it->next)
if (!it->next) return it;
return nullptr;
}
void print(std::ostream &str = std::cout) const;
void printReverse(std::ostream &str) const { printReverse(str, head); }
private:
void static printReverse(std::ostream &str, Node const* head);
};
template <class T> std::ostream &operator<<(std::ostream &str, LinkedList<T> const &data) {
data.print(str);
return str;
}
template <class T> void LinkedList<T>::print(std::ostream &str) const {
for (Node const* curr=head; curr; curr = curr->next) {
str << curr->data << "\n";
}
}
template <class T> void LinkedList<T>::printReverse(std::ostream &str, Node const* head) {
if (!head) return;
printReverse(str, head->next);
std::cout << head->data << "\n";
}
int main() {
LinkedList<int> ll;
ll.append(1);
ll.append(2);
ll.append(3);
ll.append(4);
ll.print(std::cout);
ll.printReverse(std::cout);
}
打印
1
2
3
4
4
3
2
1