《算法》一书中,在算法3.1中提到了Map的实现,这里根据书上的思想,用单向链表简单写了写。

#ifndef SEQUENTIAL_H
#define SEQUENTIAL_H
template<class K, class V>
class Link
{
private:
    class Node
    {
    public:
        K key=;
        V value=;
        Node *next=nullptr;
    public:
        Node(K, V, Node*);
        Node(){};
    };
private:
    Node *first;
    int _length;
    int _capicty;
public:
    Link();
    V& get(K key);
    void put(K key, V value);
    int Size() const;
    int Capicty() const;
    bool Delete(K key);
    ~Link();
};

template<class K, class V>
Link<K, V>::Node::Node(K key, V value, Node* node)
{
    this->key = key;
    this->value = value;
    next = node;
}
template<class K, class V>
V& Link<K, V>::get(K key)
{
    Node *x = first;
    ; i < _length&& x != nullptr; i++)
    {
        if (x->key == key) return x->value;
        x = x->next;
    }
}
//如果没有K,则在链表尾加入新的键值对
template<class K, class V>
void Link<K, V>::put(K key, V value)
{
    Node *x = first;
    bool flag = false;
    ; i < _length; i++)
    {
        if (x->key == key) { x ->value = value; flag = true; break; }
        x = x->next;
    }
    if (!flag)
    {
         >= _capicty)
        {
            Node *m = first;
            _capicty *= ;
            Node **n = new Node*[_capicty];
            ; i < _capicty; i++)
            {
                n[i] = new Node();
            }
            ; i < _capicty - ; i++)
            {
                n[i]->next = n[i + ];
            }
            ; i < _capicty && m!=nullptr; i++)
            {
                n[i] =  m;
                m = m->next;
            }
            delete[] first;
            first = n[];
        }
        int l = _length;
        Node *y = first;
        while (l)
        {
            y = y->next;
            l--;
        }
        y->value = value;
        y->key = key;
        y->next = y->next;
        _length++;
    }

}
template<class K, class V>
Link<K, V>::~Link()
{
    if (first != nullptr) delete[] first;
}
template<class K, class V>
Link<K, V>::Link()
{
    Node** n = ];
    _capicty = ;
    _length = ;
    ; i <  ; i++)
    {
        n[i] = new Node();
    }
    ; i < -; i++)
    {
        n[i]->next = n[i+];
    }
    first = n[];
}
template<class K, class V>
int Link<K, V>::Size() const
{
    return _length;
}

//当前链表容量
template<class K, class V>
int Link<K, V>::Capicty() const
{
    return _capicty;
}
template<class K, class V>
bool Link<K, V>::Delete(K key)
{
    bool flag = false;

    Node* n = first->next;
    Node* b = first;
    if (b->key == key)
    {
        _length--;
        first = first->next;
        return true;
    }
    ; i < _length; i++)
    {
        if (n->key == key)
        {
            b->next = n->next;
            _length--;
            flag = true;
            break;
        }
        b = b->next;
        n = n->next;
    }
    return flag;
}
#endif

C++链表与键值对的更多相关文章

  1. (转)C#中键值对类型Hashtable与Dictionary比较和相关用法

    最近在使用C#中的Hashtable与Dictionary的时候,想知道其区别,通过查找网络相关博客资料,作出下列总结. Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是 ...

  2. 浅谈Redis数据库的键值设计(转)

    丰富的数据结构使得redis的设计非常的有趣.不像关系型数据库那样,DEV和DBA需要深度沟通,review每行sql语句,也不像memcached那样,不需要DBA的参与.redis的DBA需要熟悉 ...

  3. JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)

    Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...

  4. 从源码看HashMap键值对集合

    之前我们看过了两种类型的集合,ArrayList集合和LinkedList集合,两种集合各有优势,我们不具体说了,但是本篇要看的集合可以完成它们完成不了的任务.比如:现有一篇文章,要你统计其中出现了哪 ...

  5. [Redis]Redis的五种数据类型与键值/服务器相关命令

    -------------------------------------------------------------------------------------- String(字符串):最 ...

  6. 数据结构(一): 键值对 Map

    Map基本介绍 Map 也称为:映射表/关联数组,基本思想就是键值对的关联,可以用键来查找值. Java标准的类库包含了Map的几种基本的实现,包括:HashMap,TreeMap,LinkedHas ...

  7. redis基础之基本键值操作和使用(三)

    前言 redis安装完毕后开始使用redis,先熟悉命令行操作. redis数据的类型 键:redis的所有的键都是string类型: 值:五种类型 string:字符串类型:一个string最大可以 ...

  8. [19/03/26-星期二] 容器_Map(图、键值对、映射)接口之HashMap(散列映射)&TreeMap(树映射)

    一.概念&方法 现实生活中,我们经常需要成对存储某些信息.比如,我们使用的微信,一个手机号只能对应一个微信账户,这就是一种成对存储的关系. Map就是用来存储“键(key)-值(value) ...

  9. 浅谈REDIS数据库的键值设计(转)

    add by zhj: 关系数据库表的一条记录可以映射成Redis中的一个hash类型,其实数据库记录本来就是键值对.这样,要比本文中的键设计用更少的键,更节省内存,因为每个键除了它的键值占用内存外, ...

随机推荐

  1. 跨平台的WatiForSingleObject实现

    移植win32程序时,有一个难点就是涉及到内核对象的操作,需要模拟win32的实现. 其中比较奇葩的一个是WaitForSingleObject系列. Linux中没有类似的timeout实现,模拟这 ...

  2. bzoj 1053: [HAOI2007]反素数ant 搜索

    1053: [HAOI2007]反素数ant Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1497  Solved: 821[Submit][Sta ...

  3. Seven Steps to Success Machine Learning in Practice

    Seven Steps to Success Machine Learning in Practice Project failures in IT are all too common. The r ...

  4. Jqgrid动态拖拽

    //注册事件 jQuery("#list1").jqGrid('setGridParam', { gridComplete : function() { $("#_emp ...

  5. 李洪强漫谈iOS开发[C语言-033]-三元运算符的应用

  6. SQL*Net more data from client

    SQL*Net more data from client The server is waiting on the client to send more data to its client sh ...

  7. 游戏开发Camera之Cinematic Camera-深度

    人的视觉系统是二维的,它通过生理和心理的暗示来感知图像的深度,在现实世界中视觉系统会自动用深度线索depth cue来确定对象之间的距离游戏画面也是二维的,用x,y轴来定义,画面深度用z轴来定义,可以 ...

  8. Unity3d 基于物理渲染Physically-Based Rendering之最终篇

    前情提要: 讲求基本算法 Unity3d 基于物理渲染Physically-Based Rendering之specular BRDF plus篇 Unity3d 基于物理渲染Physically-B ...

  9. HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  10. Hash(4) hashtable,hashmap

    首先,我们要知道set是利使用map是实现的,因为只要利用map中的key唯一性就行了. 1.hashmap 和hashtable的区别是什么? 我们可以背出:  hashtable线程安全.hash ...