#ifndef __LIST_HPP__ #define __LIST_HPP__ #include // 先声明链表类,便于node将其声明为友元 template class SimpleList; // 链表节点类 template class node { public: node() : next(nullptr){} node(T val) : data(val), next(nullptr) {} private: T data; node* next; friend class SimpleList; }; /* 链表操作 1、删除节点 2、添加节点 3、 */ template class SimpleList { public: SimpleList(); // 构造函数 SimpleList(const SimpleList& l); // 拷贝构造 ~SimpleList(); // 析构函数 SimpleList& operator= (const SimpleList& l); // 拷贝赋值 void insert(int index, T val); // 在index处插入结点 void remove(int index); // 删除index处结点 T get_node(int index); // 获取index处结点值 int find(int value); // 查找值value,找到返回index,找不到返回-1 int size(); // 获取链表长度 void push_back(T val); // 在链表尾部插入数据 T front(); // 获取第一个 void pop_front(); // 删除第一个 void clear(); // 清空 bool empty(); T begin(); // 第一个 private: node* head_ptr; // 链表头指针 int length; // 链表长度 }; // 构造函数 template SimpleList::SimpleList() { this->head_ptr = nullptr; this->length = 0; } // 拷贝构造 template SimpleList::SimpleList(const SimpleList& l) { if(l.head_ptr == nullptr) { this->head_ptr = nullptr; this->length = 0; } else { this->head_ptr = new node; node* p1 = this->head_ptr; node* p2 = nullptr; // 拷贝链表 for(p2 = l.head_ptr; p2->next != nullptr; p2 = p2->next) { p1->data = p2->data; p1->next = new node; p1 = p1->next; } p1->data = p2->data; p1->next = nullptr; this->length = l.length; } } // 拷贝赋值 template SimpleList& SimpleList::operator= (const SimpleList& l) { if(this->head_ptr == nullptr) // 原链表空 { if(l.head_ptr == nullptr) { this->head_ptr = nullptr; this->length = 0; } else { this->head_ptr = new node; node* p1 = this->head_ptr; node* p2 = nullptr; // 拷贝链表 for(p2 = l.head_ptr; p2->next != nullptr; p2 = p2->next) { p1->data = p2->data; p1->next = new node; p1 = p1->next; } p1->data = p2->data; p1->next = nullptr; this->length = l.length; } } else // 原链表非空 { if(l.head_ptr == nullptr) { // 清空链表 node* p1 = this->head_ptr; node* p2 = p1->next; while(p2 != nullptr) { delete p1; p1 = p2; p2 = p2->next; } delete p1; this->length = 0; } else { if(l.length > this->length) // 待复制的链表更长,先将原链表填满再申请空间 { node* p1 = this->head_ptr; node* p2 = l.head_ptr; for(int i = 0; i < this->length - 1; i++) { p1->data = p2->data; p1 = p1->next; p2 = p2->next; } p1->next = new node; for(int i = this->length - 1; i < l.length - 1; i++) { p1->data = p2->data; p1->next = new node; p1 = p1->next; p2 = p2->next; } p1->data = p2->data; p1->next = nullptr; } else // 待复制的链表更短或相等,更短时需要释放原链表多余空间 { node* p1 = this->head_ptr; node* p2 = l.head_ptr; // 复制数据 for(int i = 0; i < l.length; i++) { p1->data = p2->data; p1 = p1->next; p2 = p2->next; } // 删除多余结点 while(p1 != nullptr) { p2 = p1->next; delete p1; p1 = p2; } } } this->length = l.length; } return *this; } // 在index处插入结点 template void SimpleList::insert(int index, T val) { if((index > this->length)) // 超过索引,最多可以插到当前结点的下一个结点,否则就是超过索引 { throw runtime_error("index out of this SimpleList`s range"); } else if((this->head_ptr == nullptr) && (index == 0)) // 插在空链表的头 { this->head_ptr = new node; this->head_ptr->next = nullptr; this->head_ptr->data = val; this->length++; } else // 一般情况 { node* p1 = this->head_ptr; node* p2 = new node; for(int i = 0; i < index - 1; i++) { p1 = p1->next; } p2->data = val; p2->next = p1->next; p1->next = p2; this->length++; } } // 删除index处结点 template void SimpleList::remove(int index) { node* p1 = this->head_ptr; node* p2 = nullptr; for(int i = 0; i < index - 1; i++) { p1 = p1->next; } p2 = p1->next->next; delete p1->next; p1->next = p2; this->length--; } // 获取index处结点值 template T SimpleList::get_node(int index) { if(index > this->length - 1) // 超过索引 { throw runtime_error("index out of this SimpleList`s range"); } node* p1 = this->head_ptr; for(int i = 0; i < index; i++){ p1 = p1->next; } return p1->data; } // 获取第一个 template T SimpleList::front() { return get_node(0); } // 删除index处结点 template void SimpleList::pop_front() { remove(0); } // 删除index处结点 template void SimpleList::clear() { node* p; while (head_ptr != NULL) { p = head_ptr; head_ptr = head_ptr->next; delete p; p = nullptr; } } // 删除index处结点 template T SimpleList::begin(){ return head_ptr; } // 查找值value,找到返回index,找不到返回-1 template int SimpleList::find(int value) { node* p1 = this->head_ptr; for(int i = 0; i < this->length; i++) { if(p1->data == value) { return i; } p1 = p1->next; } return -1; } // 获取链表长度 template int SimpleList::size() { return this->length; } // 链表是否为空 template bool SimpleList::empty() { return size() == 0 ? true : false ; } // 在链表尾部插入数据 template void SimpleList::push_back(T val) { if(this->head_ptr == nullptr) // 链表为空 { this->head_ptr = new node; this->head_ptr->data = val; this->head_ptr->next = nullptr; } else { node* p1 = this->head_ptr; node* p2 = new node; p2->data = val; p2->next = nullptr; while(p1->next != nullptr) { p1 = p1->next; } p1->next = p2; } this->length++; } // 析构函数 template SimpleList::~SimpleList(){ // 清空链表 clear(); } #endif