-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
172 lines (151 loc) · 2.49 KB
/
LinkedList.h
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once
template<typename T>
class Node {
public:
Node(T);
Node() {};
void insert(const std::vector<T>&);
void display();
int length();
bool exists(T);
void reverse();
void insert_at(int, T);
int delete_first();
int delete_at(T);
void insert_before_first(T);
bool operator==(const Node<T>&);
private:
std::shared_ptr<Node> head, last, prev, next;
int data = 0;
};
template<typename T>
bool
Node::operator==(const Node<T>& rh) {
}
template<typename T>
bool
Node<T>::exists(T data) {
auto p = head;
while (p) {
if (p->data == data) return true;
p = p->next;
}
return false;
}
template<typename T>
void
Node<T>::reverse() {
auto p = head;
while (p) {
auto temp = p->next;
p->next = p->prev;
p->prev = temp;
p = p->prev;
// last node?
if (!p && !p->next) {
head = p;
}
}
}
template<typename T>
int
Node<T>::delete_at(T pos) {
if (pos >= length()) {
throw std::runtime_error{ "Wrong len" };
}
auto p = head;
// advance to pos
for (int i = 0; i < pos - 1; i++) {
p = p->next;
}
p->prev->next = p->next;
//any nodes after this one?
if (p->next) {
p->next->prev = p->prev;
}
auto x = p->data;
p.reset();
return x;
}
template<typename T>
int
Node<T>::delete_first() {
auto p = head;
head = head->next;
if (head) {
head->prev.reset();
}
return p->data;
}
template<typename T>
void
Node<T>::insert_at(int pos, T d) {
if (pos >= length()) {
throw std::runtime_error{ "Wrong position" };
}
if (pos == 0) {
insert_before_first(d);
return;
}
auto t = std::make_shared<Node>();
t->data = d;
// advance to pos
auto p = head;
for (auto i = 0; i < pos - 1; i++) {
p = p->next;
}
t->next = p->next;
t->prev = p;
if (p->next) {
p->next->prev = t;
}
p->next = t;
}
template<typename T>
void
Node<T>::insert_before_first(T data) {
auto t = std::make_shared<Node>();
t->data = data;
t->next = head;
head->prev = t;
head = t;
}
template<typename T>
int
Node<T>::length() {
int len = 0;
auto p = head;
while (p) {
len++;
p = p->next;
}
return len;
}
template<typename T>
Node<T>::Node(T data) {
this->data = data;
head = std::make_shared<Node>();
}
template<typename T>
void
Node<T>::display() {
auto p = head;
while (p) {
std::cout << p->data << '\t';
p = p->next;
}
std::cout << std::endl;
}
template<typename T>
void
Node<T>::insert(const std::vector<T>& v) {
last = head;
for (auto val : v) {
auto t = std::make_shared<Node>();
t->data = val;
t->next = last->next;
t->prev = last;
last->next = t;
last = t;
}
}