数据结构设计

  LRU

#include <iostream>
#include <algorithm>
#include <list>
#include <map>
using namespace std;

const int INF=0x3f3f;

template <typename k,typename v>
class Node
{
    public:
        Node()
        {
            key=0;
            value=0;
        }
        Node(int k1,int v1):key(k1),value(v1){}
    private:
        k key;
        v value;
};

template <typename k,typename v>
class MyCache
{
    public:
        MyCache(int capacity)
        {
            this->capacity=capacity;
        }
        v get(const k &key);
        void set(const int &key,const int &value);
    private:
        map<k,Node<k,v> > map;
        list<Node<k,v> >list;
        int capacity;
    friend class Node<k,v>;
};

template <typename k,typename v>
v MyCache<k,v>::get(const k &key)
{
    if(map.count(key)==1)
    {
        auto res=find(list.begin(),list.end(),key);
        Node<k,v> node;
        node.k=key;
        node.v=map.at(key);
        list.erase(res);
        list.push_back(node);
        return node.v;
    }
    return -INF;
}

template <typename k,typename v>
void MyCache<k,v>::set(const int &key,const int &value) 
{
    if(map.count(key)==1)
    {
        Node<k,v> node;
        node.key=key;
        node.value=value;
        auto res=find(list.begin(),list.end(),key);
        list.erase(res);
        list.push_back(node);
    }
    else
    {
        Node<k,v> node(key,value);
        map.insert(node);
        list.push_back(node);
        if(map.size()==capacity+1)
        {
            list.erase(list.begin());
            map.erase((*(list.begin()).k));
        }
    }
}
int main()
{
    return 0;
}

  LFU

#include <iostream>
#include <algorithm>
#include <list>
#include <ext/hash_map>
using namespace std;

class LFU;
class Node
{
    friend class LFU;
    private:
        int key;
        int value;
        int times;
    public:
        Node()
        {
            key=0;
            value=0;
            times=0;
        }
        Node(int k,int v,int t)
        {
            this->key=k;
            this->value=v;
            this->times=t;
        }
};

class LFU
{    
    public:
        LFU(int c)
        {
            this->size=0;
            this->capacity=c;
        }
        void set(const int &key,const int &vaule);
    private:
        int capacity;
        int size;
        hash_map<int,Node> recoder;
        has_hmap<Node,list<Node> > head;
        
        void moves(const Node &node);
};
void LFU::moves(const Node &node)
{
    
}
void LFU::set(const int &key,const int &value)
{
    if(recoder.count(key)==1)
    {
        Node node;
        node.key=key;
        node.value=value;//value赋值为新的value 
        ++(node.times);
        
        recoder.erase(key);
        recoder.insert({key,node});
        
        moves(node);
    }
    else
    {
        if(size==capacity)
        {
            //删词频最低的链表的尾部的结点
            
        }
    }
}
int main()
{
    
    
    return 0;
}

  表达式求和

#include <iostream>
#include <list>
#include <string>
#include <cstdlib>
using namespace std;

typedef struct Result
{
    int res;
    int pos;
    Result()
    {
        res = 0;
        pos = 0;
    }
}Result;

class Sum
{
public:
    int sum_for_string(const string &str);
private:
    Result value(const string &str, unsigned int pos);
    void add_num(list<string> &ls, int pre);
    int get_num(list<string> &ls);
};
int Sum::sum_for_string(const string &str)
{
    Result res=value(str, 0);
    return res.res;
}
Result Sum::value(const string &str, unsigned int i)
{
    list<string> ls;
    Result res;
    int pre = 0;
    while (i < str.length() && str.at(i) != ')')
    {
        if (str.at(i) > '0'&&str.at(i) < '9')//处理数字
            pre = pre * 10 + str.at(i++) - '0';
        else if (str.at(i) != '(')//处理符号 + - * /
        {
            add_num(ls, pre);
            ls.push_back(str.substr(i, 1));
            i++;
            pre = 0;
        }
        else//遇到( 递归处理
        {
            res = value(str, i + 1);
            pre = res.res;
            i = res.pos + 1;
        }
    }
    add_num(ls, pre);//把最后一个数加入链表
    res.res = get_num(ls);
    res.pos = i;
    return res;
}
void Sum::add_num(list<string>& ls, int pre)
{
    if (!ls.empty())
    {
        if (ls.back() != "+"&&ls.back() != "-")// * /
        {
            string t = ls.back();//取符号
            ls.pop_back();

            string cur = ls.back();//取数字
            ls.pop_back();

            int curr = strtol((cur.c_str()), nullptr,10);
            pre = (t == "*" ? curr * pre : curr / pre);
        }
    }
    ls.push_back(to_string(pre));
}
int Sum::get_num(list<string>& ls)
{
    int res = 0;
    bool add = true;
    for (auto it = ls.begin(); it != ls.end(); ++it)
    {
        int num = 0;
        if ((*it) == "+")
            add = true;
        else if ((*it) == "-")
            add = false;
        else
        {
            num = strtol(((*it).c_str()), 0, 10);
            res += add ? num : (-num);
        }
    }
    return res;
}
int main()
{
    string str("1+2+3*3+(3+1)/2");
    Sum s;
    cout<<s.sum_for_string(str)<<endl;
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/tianzeng/p/10574489.html