数据结构设计

  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、已知一个链表中存储了若干名学生的信息,每名学生的信息包括:学号、英语成绩、数学成绩、计算机成绩。 现编写一个函数search(),要求对输入的无序学号进行排序,然后采用折半查找方法查找输入学生学号,并输出该学生各科成绩。 2、设计一个学生类(CStudent),它具有私有数据成员是:学号、姓名、数学、外语和计算机课程的成绩。要求能实现求三门课总成绩和平均成绩,并能设置和显示学生信息 (类声明和成员函数定义分离)。设计一个友元函数,按照成绩从高到低的顺序输出姓名、学号和成绩信息。 3、实现雇员管理,类Employee需存储雇员的姓名。这种信息对于所有雇员(包括Employee的派生类的雇员)是很普遍的。现在假设从雇员类Employee派生出了小时工类HourlyWorker、计件工类PieceWorker、老板类Boss和销售员类CommissionWorker。小时工每周工作40小时,超过40小时部分的报酬是平时的1.5倍;计件工是按生产的工作件数计算报酬的,每件的报酬是固定的,假设他只生成一种类型的工件,因而类PieceWorker的private数据成员是生产的工件数量和每件的报酬;老板每周有固定的薪水;销售员每周有小部分固定的基本工资加上其每周销售额的固定百分比。设计和规划该类体系,并分别产生每个基类及派生类对象,并显示该员工的工资。 4、约瑟夫生死者游戏 每30个旅客同乘一条船,因为严重超载,加上风高浪大,危险万分;因此船长告诉乘客,只有将全船一半的旅客投入海中,其余人才能幸免遇难。无奈,大家只得同意这种办法,并议定30个人围成一圈,由第一个人数起,依次报数,数到第9人,便把他投入大海中,然后再从他的下一个人数起,数到第9人,再将他扔进大海中,如此循环地进行,直到剩下15个乘客为止。问哪些位置是将是被扔下大海的位置。 5、求二叉树上结点的路径 要求在采用链式存储结构存储的二叉树上,以bt指向根结点,p指向任一给定的结点,编程实现求出从根节点到给定结点之间的路径。 6、图的操作 (1)写出将一个无向图的邻接矩阵转换成邻接表的算法 (2)设计一个算法,判断无向图G是否连通。若连通则返回1; 返回0。 7、内部排序算法的性能分析 要求:(1)对冒泡排序、直接排序、简单选择排序、快速排序、希尔排序、堆排序算法进行比较; (2)待排序表的表长不小于100,表中数据随机产生,至少用5组不同数据作比较,比较指标有:关键字参加比较次数和关键字的移动次数(关键字交换记为3次移动); (3)输出各种算法的排序结果和比较结果。 8.2、通讯录管理系统 编程实现通讯录管理系统,要求该系统能够完成通讯信息的建立、查询、插入、删除等基本功能。程序运行后至少给出下面7个菜单项的选择并分别实现其功能: 0、 通讯录的建立 1、通讯录信息输出 2、 通讯者结点信息的删除 3、通讯者结点信息的查询 4、 通讯者结点信息的插入 5、通讯录信息更改 6、 退出通讯录管理系统 设计的任务要求,通讯录中每个学生的基本信息应包括姓名、地址、电话等基本信息,采用链表存储结构。(复习c语言结构体和链表知识)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值