0% found this document useful (0 votes)
31 views6 pages

Linked List - Final

The document describes a linked list data structure implemented as a C++ template class. The class includes methods for adding elements to the front and back of the list, inserting elements at a specified index, removing elements from the front, back or a specified index, and clearing the entire list. It also includes a size variable to track the number of elements and overloads the [] operator to access elements by index.

Uploaded by

Andrew Vakulyuk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Linked List - Final

The document describes a linked list data structure implemented as a C++ template class. The class includes methods for adding elements to the front and back of the list, inserting elements at a specified index, removing elements from the front, back or a specified index, and clearing the entire list. It also includes a size variable to track the number of elements and overloads the [] operator to access elements by index.

Uploaded by

Andrew Vakulyuk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

### part 3

#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();
}

int GetSize() { return size; }


void push_front(T data);
void insert(T value, int index);
void push_back(T data);
void pop_front();
void removeAt(int index);
void pop_back();

T& operator [](const int index);


void clear();

private:
template<typename T>
class Node {
public:
T data;
Node* pNext;

Node(T data = T(), Node* pNext = nullptr) {


this->data = data;
this->pNext = 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;

current->pNext = new Node<T>(data);


}
size++;
}

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);

for (int i = 0; i < lst.GetSize(); i++) {


cout << lst[i] << endl;
}

cout << "\ninsert!\n" << endl;


lst.insert(245, 2);
cout << "\nremove!\n" << endl;
lst.removeAt(4);

for (int i = 0; i < lst.GetSize(); i++){


cout << lst[i] << endl;
}

cout << "\npop_back\n";


lst.pop_back();

for (int i = 0; i < lst.GetSize(); i++) {


cout << lst[i] << endl;
}

cout << "\nelemenets on the list: " << lst.GetSize() << endl;

cout << "clear" << endl;


lst.clear();

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();
}

int GetSize() { return size; }


T& operator [] (const int index);

void push_front(T data);


void insert(T data, int index);
void push_back(T data);

void pop_front();
void removeAt(int index);
void pop_back();

void clear();

private:
template <typename T>
class Node {
public:
T data;
Node* pNext;

Node(T data = T(), Node* pNext = nullptr) {


this->data = data;
this->pNext = 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;
}
```

You might also like