forked from adi1998/CSN-102
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
211 lines (170 loc) · 4.73 KB
/
LinkedList.cpp
File metadata and controls
211 lines (170 loc) · 4.73 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<iostream>
#include<string>
using namespace std;
template <class T>
class Node{
public:
Node(T data, Node<T> *link);
~Node();
T data;
Node<T> *link;
};
template <typename T>
Node<T>::Node(T data, Node<T> *link){
this->data = data;
this->link = link;
}
template <typename T>
Node<T>::~Node(){
//cout << "Node with address " << this << " and referring to : " << this->link << " destructed" << endl;
}
template <class T>
class LinkedList{
public:
LinkedList();
~LinkedList();
bool isEmpty(); // Done
int Length() const;
bool Find(int k, T data) const; // Done
int Search(const T data) const; // Done
LinkedList<T> Delete(int k);
LinkedList<T> Insert(const T data);
LinkedList<T> Insert(int k, const T data);
T ElementAt(int index);
void PrintPointerTable();
int length;
private:
Node<T> *first = NULL;
Node<T> *last = NULL;
Node<T> NodeAt(int index);
int listSize;
};
template <typename T>
LinkedList<T>::LinkedList(){
length = 0;
}
template <typename T>
LinkedList<T>::~LinkedList(){
//cout << "Object destructed!" << endl;
}
template <typename T>
bool LinkedList<T>::isEmpty(){
return (length == 0);
}
template <typename T>
bool LinkedList<T>::Find(int k, T data) const{
Node<T> *current = this->first;
int index = 0;
while (current->link != NULL && current->data !=data){
index++;
}
if (current->link == NULL){
return -1;
}
return index;
}
template <typename T>
int LinkedList<T>::Search(const T data) const{
Node<T> *current = this->first;
int index = 0;
while (current->data != data){
index++;
}
if (current->link == NULL){
return -1;
}
return index;
}
template <typename T>
LinkedList<T> LinkedList<T>::Insert(const T data){
Node<T> *node = new Node<T>(data, NULL);
if (length == 0){
first = node;
last = node;
} else {
last->link = node;
last = node;
}
length++;
return *this;
}
template <typename T>
LinkedList<T> LinkedList<T>::Insert(int k, const T data){
if (k < 0 || k >= length)
throw std::out_of_range("Invalid index");
int index = 0;
Node<T> *current = this->first;
while (index != k-1){
current = current->link;
index++;
}
cout << "current address " << ¤t << " and points to " << current->link << endl;
Node<T> *node = new Node<T>(data, current->link);
current->link = node;
cout << "Node points at " << node->link << " and has address " << &node << endl;
cout << "current address " << ¤t << " and points to " << current->link << endl;
if (k == 0)
first = node;
if (k == this->length-1)
last = node;
length++;
return *this;
}
template <typename T>
T LinkedList<T>::ElementAt(int index){
if (index < 0 || index >= length)
throw std::out_of_range("Invalid index");
Node<T> current = *first;
int counter = 0;
while (counter != index){
current = *(current.link);
counter++;
}
return current.data;
}
template <typename T>
void LinkedList<T>::PrintPointerTable(){
Node<T> *node = this->first;
while(node->link){
}
}
int main(){
LinkedList<int> linkedList;
int a[] = {213,452,987,47,19,3873,2840,2373};
int pass = 0;
int fail = 0;
for (int i = 0; i < 8; i++){
cout << "Adding " << a[i] << " to list \'linkedList\'..." << endl;
linkedList.Insert(a[i]);
cout << "length now is : " << linkedList.length << endl;
}
cout << "Printing LinkedList..." << endl;
for (int i = 0; i < linkedList.length; i++){
cout << linkedList.ElementAt(i) << " ";
}
cout << endl;
cout << "Inserting \'245\' at index \'6\''..." << endl;
linkedList.Insert(6, 245);
cout << "Verifying elements from linkedList..." << endl;
for (int i = 0; i < linkedList.length; i++){
if (a[i] == linkedList.ElementAt(i)){
cout << "Verified at " << i << " | element : " << linkedList.ElementAt(i) << endl;
pass++;
} else {
cout << "Failed verification of element at " << i << endl;
fail++;
}
}
cout << "Printing LinkedList..." << endl;
for (int i = 0; i < linkedList.length; i++){
cout << linkedList.ElementAt(i) << " ";
}
cout << endl;
cout << "Printing Array..." << endl;
for (int i = 0; i < linkedList.length - 1; i++){
cout << a[i] << " ";
}
cout << endl << endl;
cout << "Testing complete" << endl;
cout << "Passed : " << pass << " Failed : " << fail << endl;
}