desty
 
 
#pragma once
#include <iostream>
using namespace std;

template <class T>
class Node {
public:
T value;
Node<T>* next;
Node<T>* prev;
Node(T val) :value(val), next(nullptr), prev(nullptr) {};
};

template <class T>
class LinkedList
{
public:
int _size;
Node<T>* head;
Node<T>* tail;
LinkedList() :_size(0), head(nullptr), tail(nullptr) {};
~LinkedList();
bool empty() { return head == nullptr; }
void push_front(T val);
void push_back(T val);
void print();
void remove(T val);
void removeNum(int n);
void insert(T val);
T& front();
T& back();
T& pop_front();
T& pop_back();
};

template<class T>
void LinkedList<T>::push_front(T val) {
Node<T>* new_node = new Node<T>(val);
if (empty()) {
head = tail = new_node;
}
else {
head->prev = new_node;
new_node->next = head;
head = new_node;
}
_size++;
}

template<class T>
void LinkedList<T>::push_back(T val) {
Node<T>* new_node = new Node<T>(val);
if (empty()) {
tail = head = new_node;
}
else {
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
}
_size++;
}

template<class T>
T& LinkedList<T>::front() {
if (empty()) {
throw "list is empty, has not first node";
}
return head->value;
}

template<class T>
T& LinkedList<T>::back() {
if (empty()) {
throw "list is empty, has not last node";
}
return tail->value;
}

template<class T>
T& LinkedList<T>::pop_front() {
if (empty()) {
throw "list is empty, can't pop";
}
T result = head->value;
if (head == tail) {
delete head;
head = tail = nullptr;
}
else {
Node<T>* del_node = head;
head->next->prev = nullptr;
head = head->next;
delete del_node;
}
_size--;
return result;
}

template<class T>
T& LinkedList<T>::pop_back() {
if (empty()) {
throw "list is empty, can't pop";
}
T result = tail->value;
if (head == tail) {
delete tail;
tail = head = nullptr;
}
else {
Node<T>* del_node = tail;
tail->prev->next = nullptr;
tail = tail->prev;
delete del_node;
}
_size--;
return result;
}

template <class T>
void LinkedList<T>::print() {
cout << "List: ";
Node<T>* tmp = head;
while (tmp) {
cout << tmp->value << " ";
tmp = tmp->next;
}
cout << "end list" << endl;
}

template <class T>
void LinkedList<T>::remove(T val) {
Node<T>* tmp = head;
while (tmp && (tmp->value != val)) {
tmp = tmp->next;
}
if (tmp == nullptr) {
return;
}
if (tmp == head) {
pop_front();
return;
}
if (tmp == tail) {
pop_back();
return;
}
tmp->next->prev = tmp->prev;
tmp->prev->next = tmp->next;
delete tmp;
_size--;
}



template <class T>
void LinkedList<T>::insert(T val) {
Node<T>* tmp = head;
while (tmp && (tmp->value < val)) {
tmp = tmp->next;
}
if (tmp == head) {
push_front(val);
return;
}
if (tmp == nullptr) {
push_back(val);
return;
}
Node<T>* new_node = new Node<T>(val);
tmp->prev->next = new_node;
new_node->next = tmp;
new_node->prev = tmp->prev;
tmp->prev = new_node;
_size++;
}
Game Collector
Rarest Achievement Showcase
< >
Comments
Asya Dec 31, 2020 @ 10:53am 
Хэй, дорогой мой!
С Наступающим новым годом тебя, пусть все плохое и плохие людишки останутся в прошлом году, забери все самое ценное и дерзай в Новом году новые свершения, удивляй и достигай свои цели! Ты все смогешь:3
:healthvial: на удачу