[Fixed] should i use initialization list?
Code:
#include <iostream>
template <typename Type>
class LinkList {
struct Node {
Type item;
Node *next;
// Should i use initialization list here?
// Which is better? (safer? faster?)
Node(Type i, Node *n=0) {
item = i;
next = n;
}
} *root;
public:
LinkList() : root(0) {}
void add(Type item) {
if (root) {
Node *tmp = root;
while (tmp->next) tmp = tmp->next;
tmp->next = new Node(item);
} else {
root = new Node(item);
}
}
void print() {
std::cout << "List items: ";
for (Node *tmp = root; tmp; tmp = tmp->next) {
std::cout << tmp->item << " ";
}
std::cout << std::endl;
}
};
int main()
{
LinkList<int> list;
int choice, item;
while (true) {
std::cout << "1) Add\n2) Print\n0) Quit\n? ";
std::cin >> choice;
if (choice == 1) {
std::cin >> item;
list.add(item);
} else if (choice == 2) {
list.print();
} else if (choice == 0) {
break;
}
}
return 0;
}