-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
55 lines (54 loc) · 1.13 KB
/
LinkedList.h
File metadata and controls
55 lines (54 loc) · 1.13 KB
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
48
49
50
51
52
53
54
55
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
struct Node {
int value;
Node* next;
Node() : value(0), next(nullptr){}
Node(int v) : value(v), next(nullptr) {}
};
class LinkedList {
private:
Node* head;
Node* tail;
public:
void insert(Node* n) {
if (head == nullptr) {
head = n;
tail = n;
}
else {
tail->next = n;
tail = n;
}
}
void remove() {
Node* temp = head;
head = head->next;
delete temp;
temp = nullptr;
}
Node* getHead() {return head;}
Node* getTail() {return tail;}
LinkedList() : head(nullptr), tail(nullptr) {}
LinkedList(Node* h) : head(h), tail(nullptr) {}
~LinkedList() {
while (head != nullptr) {
remove();
}
}
};
#endif
/*LinkedList ll;
for (int i = 0; i < 10; i++) {
Node* in = new Node(i);
ll.insert(in);
}
Node* curr = ll.getHead();
while (curr != nullptr) {
std::cout << curr->value << std::endl;
curr = curr->next;
}
for (int i = 0; i < 10; i++) {
ll.remove();
}
*/