-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
187 lines (161 loc) · 3.15 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef PROGRAM5_LINKEDLIST_H
#define PROGRAM5_LINKEDLIST_H
#include <iostream>
template <typename T>
struct Node
{
T data;
Node *next;
};
template <typename T>
class LinkedList {
public:
LinkedList();
~LinkedList();
LinkedList(const LinkedList ©);
LinkedList& operator=(const LinkedList&);
void insert(T);
void remove(T);
T at(int index);
void display();
int getSize();
private:
int size;
Node<T> *head;
Node<T> *tail;
};
template <typename T>
LinkedList<T>::LinkedList()
{
head = nullptr;
tail = nullptr;
size = 0;
}
template <typename T>
LinkedList<T>::~LinkedList()
{
auto curr = this->head;
auto currNext = curr->next;
for(int i = 0; i < size; i++)
{
delete curr;
curr = currNext;
if(curr != nullptr)
{
currNext = curr->next;
}
}
}
template <typename T>
LinkedList<T>::LinkedList(const LinkedList ©)
{
auto curr = copy.head;
size = copy.size;
head = nullptr;
tail = nullptr;
while(curr != nullptr)
{
auto currNext = curr->next;
auto *newNode = new Node<T>();
newNode->data = curr->data;
newNode->next = nullptr;
if(head == nullptr)
{
head = newNode;
tail = newNode;
} else
{
tail->next = newNode;
tail = newNode;
}
curr = currNext;
}
}
template <typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList ©)
{
auto curr = head;
size = copy.size;
while(curr != nullptr)
{
auto currNext = curr->next;
delete curr;
curr = currNext;
}
curr = copy.head;
while(curr != nullptr)
{
auto currNext = curr->next;
auto *node = new Node<T>();
node->data = curr->data;
node->next = nullptr;
if(head == nullptr)
{
head = node;
tail = node;
} else
{
tail->next = node;
tail = node;
}
curr = currNext;
}
}
template <typename T>
T LinkedList<T>::at(int index)
{
auto currNode = this->head;
if(index >= size || index < 0)
{
std::cout << "Index out of range" << std::endl;
exit(1);
}
else {
for (int i = 0; i < index; i++) {
currNode = currNode->next;
}
return currNode->data;
}
}
template <typename T>
void LinkedList<T>::insert(T data)
{
auto node = new Node<T>();
node->data = data;
node->next = nullptr;
if(head != nullptr)
{
tail->next = node;
tail = node;
} else{
head = node;
tail = node;
}
size++;
}
template <typename T>
void LinkedList<T>::remove(T data)
{
if(head != nullptr)
{
Node<T> *temp = head;
head = head->next;
delete temp;
}
}
template <typename T>
void LinkedList<T>::display()
{
auto curr = this->head;
while(curr != nullptr)
{
curr->data->display();
curr = curr->next;
}
}
template <typename T>
int LinkedList<T>::getSize()
{
return size;
}
#endif //PROGRAM5_LINKEDLIST_H