链表
#ifndef NODE_H #define NODE_H template <typename T> class node{ public: T data; node<T> *next; public: node(T _data=0) : data(_data),next(nullptr){} }; #endif // NODE_H
#ifndef LINK_H #define LINK_H #include <iostream> #include "Node.h" using namespace std; template <typename T> class Link { public: Link() { head = new node<T>; length = 0; } Link(int num) { head = new node<T>; length = 0; for(int i=0;i<num;i++) insert(0); } virtual ~Link() { int _length = length; for(int i=0;i<_length;i++) { erase(0); } delete head; } Link(const Link& rhs) { head = new node<T>; length = 0; node<T> *_node = rhs.head; for(int i=0;i<rhs.length;i++) { insert(_node->next->data); _node = _node->next; } } Link& operator=(const Link& rhs) { if(this == &rhs) return *this; int _length = this->length; for(int i=0;i<_length;i++) { erase(0); } head->next = nullptr; node<T> *_node = rhs.head; for(int i=0;i<rhs.length;i++) { insert(_node->next->data); _node = _node->next; } return *this; } public: void insert(const T&_data) { insert(_data,length); } void insert(const T&_data,int index) { if(index < 0 || index > length) return; node<T> *new_node = new node<T>(_data); if(index == 0) { new_node->next = head->next; head->next = new_node; } else if(index == length) { node<T> *_back = this->end(); _back->next = new_node; } else { node<T> *_prev = find(index-1); new_node->next = _prev->next; _prev->next = new_node; } length++; } void erase(int index) { if(index < 0 || index >= length) return; if(length <= 0) return; if(index == 0) { node<T> *del_node = head->next; head->next = del_node->next; delete del_node; } else if(index == length-1) { node<T> *prev = find(index-1); node<T> *del_node = find(index); prev->next = nullptr; delete del_node; } else { node<T> *prev = find(index-1); node<T> *del_node = find(index); prev->next = del_node->next; delete del_node; } length--; } void push_back(const T&_data) { insert(_data,length); } void push_front(const T&_data) { insert(_data,0); } void pop_back() { erase(length-1); } void pop_front() { erase(0); } node<T>* find(const int index) { if(index < 0 || index >= length) return nullptr; int _index = index; node<T> *_node = head; while(_index >= 0) { _node = _node->next; _index--; } return _node; } node<T>* begin() { if(empty()) return nullptr; return find(0); } node<T>* end() { if(empty()) return nullptr; int _length = length-1; return find(_length); } inline int size() const { return length; } inline bool empty() const { if(length == 0) return true; return false; } friend ostream& operator <<(ostream &os, const Link<T> &list) { node<T> *_node = list.head->next; while(_node != nullptr) { os << _node->data << ","; _node = _node->next; } os << std::endl; return os; } T &front() { return begin()->data; } T &back() { return end()->data; } void inverse()//逆置 { if(empty()) return; node<T>* p = head; node<T>* s = p->next; while(s->next != nullptr) { node<T> *t = s->next; s->next=p; p=s; s=t; } s->next = p; head->next->next=nullptr; head->next=s; } private: node<T> *head; int length; }; #endif // LINK_H