I am looking for pointers / comments / critiques regarding the below code. One major flaw I know is that I need to define Nodes first and then manually join them to create a List.
Are there any flaws apart from that? C++ programming style errors? RAII?
#include<iostream>
template<typename T>
class Node {
private:
T data_;
public:
Node<T>* prev;
Node<T>* next;
const T& getData() const { return data_; }
T& setData() { return data_; }
const T& setData(const T& data) {
data_ = data;
return data_;
}
Node() {
prev = NULL;
next = NULL;
}
};
template<typename T>
class List {
private:
Node<T>* head_;
Node<T>* tail_;
public:
List() {
head_ = NULL;
tail_ = NULL;
}
Node<T>* getHead() const { return head_; }
Node<T>* getTail() const { return tail_; }
Node<T>* addNode(Node<T> & rhs) {
if (tail_) {
tail_->next = &rhs;
tail_ = tail_->next;
} else {
tail_ = &rhs;
head_ = tail_;
}
return tail_;
}
};
int main() {
List<int> li;
Node<int> temp1;
temp1.setData(5);
li.addNode(temp1);
Node<int> temp2;
temp2.setData(10);
li.addNode(temp2);
Node<int>* h = li.getHead();
while(h) {
std::cout << h->getData() << std::endl;
h = h->next;
}
}