Linked List - Final
Linked List - Final
Linked List - Final
#DynamicStructure
[[Linked List]] [[Linked List.Clear]] [[Linked List.Final]] [[Linked List Double
(!!!)]]
---
```cpp
#include <iostream>
using namespace std;
template<typename T>
class List {
public:
List() {
size = 0;
head = nullptr;
}
~List() {
clear();
}
private:
template<typename T>
class Node {
public:
T data;
Node* pNext;
int size;
Node<T>* head;
};
//operator overloading
template<typename T>
T& List<T>::operator [](const int index) {
int counter = 0;
Node<T>* current = head;
while (current->pNext != nullptr) {
if (counter == index)
return current->data;
current = current->pNext;
counter++;
}
}
//elements operations
template<typename T>
void List<T>::push_front(T data) {
head = new Node<T>(data, head);
size++;
}
template<typename T>
void List<T>::insert(T value, int index) {
if (index == 0)
push_front(value);
else {
Node<T>* previous = head;
for (int i = 0; i < index - 1; i++) {
previous = previous->pNext;
}
previous->pNext = new Node<T>(value, previous->pNext);
size++;
}
}
template<typename T>
void List<T>::push_back(T data) {
if (head == nullptr)
head = new Node<T>(data);
else {
Node<T>* current = head;
while (current->pNext != nullptr)
current = current->pNext;
template<typename T>
void List<T>::pop_front() {
Node<T>* temp = head;
head = head->pNext;
delete temp;
size--;
}
template<typename T>
void List<T>::removeAt(int index) {
if (index == 0)
pop_front();
else {
Node<T>* previous = head;
for (int i = 0; i < index - 1; i++) {
previous = previous->pNext;
}
Node<T>* toDelete = previous->pNext;
previous->pNext = toDelete->pNext;
delete toDelete;
size--;
}
}
template<typename T>
void List<T>::pop_back(){
removeAt(size - 1);
}
template<typename T>
void List<T>::clear() {
while (size)
pop_front();
}
int main() {
List<int> lst;
lst.push_front(4);
lst.push_front(3);
lst.push_front(7);
lst.push_front(8);
lst.push_front(53);
lst.push_front(83);
cout << "\nelemenets on the list: " << lst.GetSize() << endl;
cout << "elements on the list: " << lst.GetSize() << endl;
return 0;
}
```
## More examples
```cpp
#include <iostream>
using namespace std;
template<typename T>
class List {
public:
List() {
size = 0;
head = nullptr;
}
~List() {
clear();
}
void pop_front();
void removeAt(int index);
void pop_back();
void clear();
private:
template <typename T>
class Node {
public:
T data;
Node* pNext;
int size;
Node<T>* head;
};
//operator overload
template<typename T>
T& List<T>::operator[](const int index){
int counter = 0;
Node<T>* current = head;
while (current != nullptr){
if (counter == index)
return current->data;
current = current->pNext;
counter++;
}
}
//element operation
template<typename T>
void List<T>::push_front(T data) {
head = new Node<T>(data, head);
size++;
}
template<typename T>
void List<T>::insert(T data, int index) {
if (index == 0)
push_front(data);
else {
Node<T>* previous = head;
for (int i = 0; i < index - 1; i++){
previous = previous->pNext;
}
previous->pNext = new Node<T>(data, previous->pNext);
size++;
}
}
template<typename T>
void List<T>::push_back(T data) {
if (head == nullptr)
head = new Node<T>(data);
else {
Node<T>* current = head;
while (current->pNext != nullptr) {
current = current->pNext;
}
current->pNext = new Node<T>(data);
}
size++;
}
template<typename T>
void List<T>::pop_back() {
removeAt(size - 1);
}
template<typename T>
void List<T>::clear(){
while (size)
pop_front();
}
template<typename T>
void List<T>::pop_front(){
Node<T>* temp = head;
head = head->pNext;
delete temp;
size--;
}
template<typename T>
void List<T>::removeAt(int index){
if (index == 0)
pop_front();
else {
Node<T>* previous = head;
for (int i = 0; i < index - 1; i++){
previous = previous->pNext;
}
Node<T>* ToDelete = previous->pNext;
previous->pNext = ToDelete->pNext;
delete ToDelete;
size--;
}
}
template<typename T>
void printList(List<T>& lst) {
for (int i = 0; i < lst.GetSize(); i++) {
cout << lst[i] << endl;
}
}
int main() {
List<int> lst;
int size = 5;
for (int i = 0; i < size; i++){
lst.push_back(rand() % 10);
}
lst.insert(155, 2);
lst.removeAt(2);
printList(lst);
return 0;
}
```