码迷,mamicode.com
首页 > 其他好文 > 详细

自调整表(self-adjusting list)的实现

时间:2020-05-04 19:39:03      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:struct   col   ++   其他   val   back   elf   链表实现   out   

自调整表:所有的插入操作都发生在表的前端。

find操作:当一个元素由find访问的时候,该元素就被移到表的前端,而其他元素的相对顺序保持不变。

以下展示自调整表的数组实现和链表实现。

数组实现:

#include <iostream>

template <typename Object>
class salist
{
private:
    Object* elem;
    int theSize;
public:
    salist():elem{nullptr},theSize{0}{}
    salist(int n)
    {
        elem=new Object[n];
        theSize=n;
    }
    ~salist()
    {
        delete[] elem;
    }
    salist(const salist& rhs)
    {
        elem = new Object[rhs.theSize];
        theSize=rhs.theSize;
        for(int i=0;i!=theSize;++i)
            elem[i]=rhs.elem[i];
    }
    const salist& operator=(const salist& rhs)
    {
        if(this==&rhs)
            return *this;
        delete[] elem;
        *this(rhs);
        return *this;
    }
    void push(const Object& x)
    {
        Object* oldElem = elem;
        ++theSize;
        elem = new Object[theSize];
        elem[0]=x;
        for(int i = 1;i!=theSize;++i)
            elem[i]=oldElem[i-1];
        delete[] oldElem;
    }
    void print()
    {
        for(int i=0;i!=theSize;++i)
            std::cout << elem[i] <<  ;
        std::cout << \n;
    }
    void find(const Object& x)
    {
        int pos=-1;
        for(int i=0;i!=theSize;++i)
            if(elem[i]==x)
                pos = i;
        if(pos==-1)
            std::cout << "no element with this value\n";
        else
        {
            for(int i=pos;i!=0;--i)
                elem[i]=elem[i-1];
        }
        elem[0]=x;
    }
};

int main()
{
    salist<int> sl;
    sl.push(1);
    sl.push(2);
    sl.push(3);
    sl.print();
    sl.find(1);
    sl.print();
    salist<int> sl2(sl);
    sl2.print();
    return 0;
}

链表实现:

#include <iostream>

template <typename Object>
class salist
{
private:
    struct Node
    {
        Object data;
        Node* prev;
        Node* next;
        
        Node(const Object& d=Object(),Node* p=nullptr,Node* n=nullptr):data(d),prev(p),next(n){}        
    };
    Node* head;
    Node* tail;
    init()
    {
        head = new Node;
        tail = new Node;
        head->next=tail;
        tail->prev=head;
    }
public:
    salist()
    {
        init();
    }
    ~salist()
    {
        clear();
        delete head;
        delete tail;
    }
    salist(const salist& rhs)
    {
        init();
        Node* ptr=rhs.head->next;
        while(ptr!=rhs.tail)
        {
            push_back(ptr->data);
            ptr=ptr->next;
        }
    }
    const salist& operator=(const salist& rhs)
    {
        if(this==&rhs)
            return *this;
        clear();
        Node* ptr=rhs.head->next;
        while(ptr!=rhs.tail)
        {
            push_back(ptr->data);
            ptr=ptr->next;
        }
        return *this;
    }
    salist(const std::initializer_list<Object>& rhs)
    {
        init();
        for(auto x : rhs)
            push_back(x);
    } 
    void push(const Object& x)
    {
        head->next=head->next->prev=new Node(x,head,head->next);    
    }
    void push_back(const Object& x)
    {
        tail->prev=tail->prev->next=new Node(x,tail->prev,tail);
    }
    void clear()
    {
        Node* ptr = head->next;
        while(ptr!=tail)
        {
            head->next=ptr->next;
            delete ptr;
            ptr=head->next;
        }
        tail->prev=head;
    }
    void find(const Object& x)
    {
        Node* ptr=head->next;
        while(ptr!=tail)
        {
            if(ptr->data==x)
            {
                ptr->prev->next=ptr->next;
                ptr->next->prev=ptr->prev;
                push(x);
                break;
            }
            ptr=ptr->next;
        }
        if(ptr==tail)
            std::cout << "no element with this value\n";
        else 
            delete ptr;
    } 
    void print()
    {
        Node* ptr = head->next;
        while(ptr!=tail)
        {
            std::cout << ptr->data <<  ;
            ptr=ptr->next;
        }    
        std::cout << \n;
    }
};

int main()
{
    salist<int> sl;
    sl.push(2);
    sl.push(5);
    sl.push(6);
    sl.print();
    salist<int> sl2=sl;
    sl2.find(2);
    sl2.print();
    salist<int> sl3={1,2,3};
    sl3.print();
    return 0;
}

 

自调整表(self-adjusting list)的实现

标签:struct   col   ++   其他   val   back   elf   链表实现   out   

原文地址:https://www.cnblogs.com/lhb666aboluo/p/12827619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!