单链表常用功能实现

1 篇文章 0 订阅
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值