-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathOOPLinkedList.cpp
More file actions
212 lines (163 loc) · 5.01 KB
/
OOPLinkedList.cpp
File metadata and controls
212 lines (163 loc) · 5.01 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
212
#include <iostream>
#include <cstdlib>
/*
known issues
1. Invalid indices return -1 instead of seg fault, which may or may not be considered a good thing
2. Node class has public constructor, which enables anyone to create obj of Node class
3. No use of inheritance
4. Complexity of some function can be optimised using secondary pointer
5. LinkedList class has no overloaded constructor
6. Both classes have no iterators
Next i'll try to :
1. Use inheritance or shift 'Node' class inside 'LinkesList'
2. OverLoad constructor of LinkedList class to initilise at time of object creation
3. optimising functions by introducing another pointer in Node class
*/
class Node // parent class for containing data members
{
private:
int data;
Node *next;
public:
// no constructor with empty parameter
Node(const int value) : data(value), next(nullptr) {}
// using initlizer list to assign values while creating constructor
const int getData() const // function to return data
{
return data;
}
Node *getNext() const // function to return next node
{
return next;
}
void setData(const int value) // function to set/update data
{
data = value;
}
void setNext(Node *obj) // function to set/update next
{
next = obj;
}
void print() const // function to print the data member
{
std::cout << data << std::endl;
}
};
class LinkedList
{
private :
size_t length;
Node* head, *tail, *temp;
public :
// default contructor
LinkedList() : head(nullptr), tail(nullptr), temp(nullptr), length(0) {}
const int getLength() const // function to return length, complexity O(n)
{
return this->length;
}
void push_back(const int value) // function to add an element at end
{ // complexity O(1) or constant time
temp = new Node(value);
if (head == nullptr)
head = tail = temp;
else
{
tail->setNext(temp);
tail = temp;
}
++length;
}
void pop_back() // function to remove last element
{ // complexity O(n) or linear time
// as it is a single linked list, have to traverse all the way to last
if (head == nullptr)
return;
else
{
for (temp = head; temp->getNext() != tail; temp = temp->getNext());
free(tail);
tail = temp;
}
--length;
}
void new_head(const int value) // function to add a new head to list
{ // complexity O(1) or constant time
temp = new Node(value);
if (head == nullptr)
head = tail = temp;
else
{
temp->setNext(head);
head = temp;
}
++length;
}
void remove_head() // function to remove current head and make 2nd node as head
{ // complexity O(1)
if (head == nullptr)
return;
temp = head;
head = head->getNext();
free(temp);
--length;
}
const int getData(const int idx) // function to get data from node
{ // complexity O(n)
// runs on 0 bases index
if (idx < 0 || idx >= length) // invalid location
return -1;
else if (idx == 0) // head
return head->getData();
else if (idx == length - 1) // tail
return tail->getData();
else // for other indices
{
temp = head;
for (int i = idx; i > 0; --i)
temp = temp->getNext();
return temp->getData();
}
}
const int operator[](const int idx) // operator overloading to ease up getData function
{ // same as getData function
if (idx < 0 || idx >= length) // invalid location
return -1;
else if (idx == 0) // head
return head->getData();
else if (idx == length - 1) // tail
return tail->getData();
else // for other indices
{
temp = head;
for (int i = idx; i > 0; --i)
temp = temp->getNext();
return temp->getData();
}
}
LinkedList& operator+(LinkedList& obj) // concatinate passed LinkedList to first
{
this->tail->setNext(obj.head);
this->length += obj.length;
return *this;
}
bool operator==(LinkedList& obj)
{
if (this->length != obj.length) // if lengths aren't equal, so are they
return false;
for (int i = 0; i < this->length; ++i) // if length is equal, check each data member
if ((*this)[i] != obj[i]) // if data member is diff
return false;
return true;
}
};
int main()
{
LinkedList obj;
obj.push_back(10);
obj.push_back(20);
LinkedList obj2;
obj2.push_back(10);
obj2.push_back(30);
std::cout << (obj == obj2) << std::endl;
return 0;
}