template
class List {
private:
class Node{
public:
type e;
Node *next;
Node():next(NULL){}
explicit Node(type data,Node *next=NULL):e(data),next(next){}
};
Node * first;
Node * end;
long long len{};
public:
List();
~List();
List (const List &a);
void push_back(type e);
void push_front(type e);
void pop_back();
void pop_front();
type back();
type front();
void sort();
long long size();
bool empty();
void clear();
List& operator = (const List&ri);
type & search(type e);
void del(type e);
void del_all(type e);
};
template
void List::del_all(type e) {
if(len>2) {
while (first->e == e) {
Node *p = first;
first = first->next;
delete p;
}
Node *p = first, *pp = first->next;
while (pp != nullptr) {
bool fl = true;
if (pp->e == e) {
p->next = pp->next;
delete pp;
fl = false;
pp = p->next;
}
if (fl) {
p = p->next;
pp = pp->next;
}
}
}else if (first->e == e)clear();
}
template
void List::del(type e) {
if(len>2){
if(first->e == e){
Node * p = first;
first = first->next;
delete p;
}else{
Node*p=first,*pp = first->next;
while(pp!= nullptr){
if(pp->e == e){
p->next=pp->next;
delete pp;
return;
}
p = p->next;
pp = pp->next;
}
}
}else if (first->e == e)clear();
}
template
type &List::search(type e) {
Node*p=first;
while(p!= nullptr){
if(p->e == e){
return *p;
}
p = p->next;
}
return *first;
}
template
void List::pop_back() {
}
template
void List::pop_front() {
Node*p = first;
first = first->next;
delete p;
}
template
void List::clear() {
if(first!= nullptr){
Node * p = first;
while (p!= nullptr){
Node * pp = p->next;
p = pp;
}
}
}
template
bool List::empty() {
return len == 0;
}
template
long long List::size() {
return len;
}
template
void List::push_front(type e) {
if (this->first!= nullptr){
this->first = new Node(e, first);
}
else {
this->first = new Node(e, nullptr);
this->end = this->first;
}
len++;
}
template
void List::sort() {
if(len > 1){
Node*p = first;
Node*pp = first->next;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - 1 - i; ++j) {
if(pp->ee){
type temp = pp->e;
pp->e = p->e;
p->e = temp;
}
p = p->next;
pp = pp->next;
}
p = first;
pp = first->next;
}
}
}
template
List &List::operator=(const List &ri) {
if(this != &ri){
if(first != nullptr){
while(first!= nullptr){
Node*p = first;
p = p->next;
delete first;
first = p;
}
len=0;
}
Node*p = ri.first;
while (p!= nullptr){
push_back(p->e);
p = p->next;
}
}
return *this;
}
template
List::List(const List &a) {
Node*p = a.first;
first = nullptr;
end = nullptr;
while (p!= nullptr){
push_back(p->e);
p = p->next;
}
}
template
type List::front() {
return first->e;
}
template
type List::back() {
return end->e;
}
template
List::List() {
first = nullptr;
end = nullptr;
len=0;
}
template
List::~List(){
clear();
}
template
void List::push_back(type e) {
if (this->end!= nullptr){
this->end->next = new Node(e, nullptr);
this->end = this->end->next;
}
else {
this->first = new Node(e, nullptr);
this->end = this->first;
}
len++;
}