#include <iostream> using namespace std; class Node{ public: int data; Node* next; }; class List{ public: Node* head; int len; List(){ head = new Node(); len = 0; } Node* back(){//尾元素 if(!head->next) return head; Node* tmp = head; while(tmp->next) tmp = tmp->next; return tmp; } Node* front(){//头元素 return head->next ? head->next : head; } int size(){ return len; } bool insert(int pos,int num){//插入数据 if(pos > size() || pos <= 0) return false; Node* tmp = head; Node* inserted = new Node(); inserted->data = num; int time = pos-1; while(time--) tmp = tmp->next; inserted->next = tmp->next; tmp->next = inserted; len++; return true; } void merge(List* that){//合并链表 if(that == this) return; if(!that->size()) return; back()->next = that->front(); len += that->size(); } int postionof(int num){//索引 int count = 1; Node* tmp = head->next; while(tmp){ if(tmp->data == num) return count; tmp = tmp->next; count++; } return -1; } Node* at(int pos){//取得pos下结点 if(pos > size() || pos <= 0) return nullptr; Node* tmp = head; int time = pos; while(time --) tmp = tmp->next; return tmp; } Node* operator[](int pos){ return at(pos); } bool swap(int pos1, int pos2){//交换元素 int asize = size(); if(pos1>asize ||pos1 <=0 ||pos2>asize ||pos2 <=0 ) return false; if(pos1 == pos2) return true; Node* ele1 = at(pos1); Node* ele2 = at(pos2); ele1->data += ele2->data; ele2->data = ele1->data - ele2->data; ele1->data -= ele2->data; return true; } void sort(){//冒泡实现 if(empty()) return; Node* tmp; int time = size() -1; while(time --){ tmp = head->next; while(tmp->next) { if (tmp->data > tmp->next->data) { tmp->next->data += tmp->data; tmp->data = tmp->next->data - tmp->data; tmp->next->data -= tmp->data; } tmp = tmp->next; } } } void reverse(){//就地反转实现 if(empty()) return; Node* last = back(); Node* left = nullptr; Node* right = nullptr; Node* current = head->next; while(current){ right = current->next; current->next = left; left = current; current = right; } head->next = last; } bool empty(){ return head->next == nullptr; } bool elementDelete(int pos){ if(pos > size() || pos <= 0 ) return false; Node* tmp = head; int time = pos - 1; while(time--) tmp = tmp->next; tmp->next = tmp->next->next; len--; return true; } void push_range_back(int* range,int len){ while(len--){ int num = *range; push_back(num); range++; } } bool elementRangeDelete(int start,int end){ if(start > end || start <=0 || end >= size()) return false; at(start-1)->next = at(end); len -= end - start; return true; } void clean(){ head->next = nullptr; } void push_back(int num){ Node* tmp = new Node(); tmp->data = num; back()->next = tmp; len++; } void pop_back(){ elementDelete(size()); } }; int main() { List* list = new List(); cout<<"empty():"<<list->empty()<<endl; list->push_back(1); list->push_back(2); list->push_back(3); cout<<"pushback: "; for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"popback: "; list->pop_back(); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"insert: "; list->insert(1,4); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"merge: "; List* list2 = new List(); list2->push_back(5); list2->push_back(6); list->merge(list2); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"positionof(5): "<<list->postionof(5)<<endl; cout<<"swap: "; list->swap(1,2); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"reverse: "; list->reverse(); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"push_range_back({7,8,9,10,11},5): "; int arr[5] = {7,8,9,10,11}; list->push_range_back(arr,5); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"elementRangeDelete(3,5): "; list->elementRangeDelete(3,5); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"sort: "; list->sort(); for(auto i = list->front();i;i = i->next) cout<<i->data<<" "; cout<<endl; cout<<"clean:"<<endl; list->clean(); cout<<"empty:"<<list->empty()<<endl; return 0; }
单链表常用功能实现
最新推荐文章于 2022-03-03 22:13:57 发布