-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList3.cpp
More file actions
47 lines (45 loc) · 891 Bytes
/
LinkedList3.cpp
File metadata and controls
47 lines (45 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
template <class T>
class LinkedList {
struct Node {
T data;
Node* next;
};
Node* head;
Node* tail;
public:
LinkedList() {
head = NULL;
tail = NULL;
}
void add(T data) {
Node* node = new Node;
node->data = data;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
}
else {
tail->next = node;
tail = node;
}
}
void print() {
Node* node = head;
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
};
int main() {
LinkedList<int> list;
list.add(1);
list.add(2);
list.add(3);
list.print();
return 0;
}