zoukankan      html  css  js  c++  java
  • 数据结构(长期)

     C++实现双链表

    实现代码
    双向链表文件(DoubleLink.h)

    #ifndef DOUBLE_LINK_HXX
    #define DOUBLE_LINK_HXX
    
    #include <iostream>
    using namespace std;
    
    template<class T> 
    struct DNode 
    {
        public:
            T value;
            DNode *prev;
            DNode *next;
        public:
            DNode() { }
            DNode(T t, DNode *prev, DNode *next) {
                this->value = t;
                this->prev  = prev;
                this->next  = next;
               }
    };
    
    template<class T> 
    class DoubleLink 
    {
        public:
            DoubleLink();
            ~DoubleLink();
    
            int size();
            int is_empty();
    
            T get(int index);
            T get_first();
            T get_last();
    
            int insert(int index, T t);
            int insert_first(T t);
            int append_last(T t);
    
            int del(int index);
            int delete_first();
            int delete_last();
    
        private:
            int count;
            DNode<T> *phead;
        private:
            DNode<T> *get_node(int index);
    };
    
    template<class T>
    DoubleLink<T>::DoubleLink() : count(0)
    {
        // 创建“表头”。注意:表头没有存储数据!
        phead = new DNode<T>();
        phead->prev = phead->next = phead;
        // 设置链表计数为0
        //count = 0;
    }
    
    // 析构函数
    template<class T>
    DoubleLink<T>::~DoubleLink() 
    {
        // 删除所有的节点
        DNode<T>* ptmp;
        DNode<T>* pnode = phead->next;
        while (pnode != phead)
        {
            ptmp = pnode;
            pnode=pnode->next;
            delete ptmp;
        }
    
        // 删除"表头"
        delete phead;
        phead = NULL;
    }
    
    // 返回节点数目
    template<class T>
    int DoubleLink<T>::size() 
    {
        return count;
    }
    
    // 返回链表是否为空
    template<class T>
    int DoubleLink<T>::is_empty() 
    {
        return count==0;
    }
    
    // 获取第index位置的节点
    template<class T>
    DNode<T>* DoubleLink<T>::get_node(int index) 
    {
        // 判断参数有效性
        if (index<0 || index>=count)
        {
            cout << "get node failed! the index in out of bound!" << endl;
            return NULL;
        }
    
        // 正向查找
        if (index <= count/2)
        {
            int i=0;
            DNode<T>* pindex = phead->next;
            while (i++ < index) {
                pindex = pindex->next;
            }
    
            return pindex;
        }
    
        // 反向查找
        int j=0;
        int rindex = count - index -1;
        DNode<T>* prindex = phead->prev;
        while (j++ < rindex) {
            prindex = prindex->prev;
        }
    
        return prindex;
    }
    
    // 获取第index位置的节点的值
    template<class T>
    T DoubleLink<T>::get(int index) 
    {
        return get_node(index)->value;
    }
    
    // 获取第1个节点的值
    template<class T>
    T DoubleLink<T>::get_first() 
    {
        return get_node(0)->value;
    }
    
    // 获取最后一个节点的值
    template<class T>
    T DoubleLink<T>::get_last() 
    {
        return get_node(count-1)->value;
    }
    
    // 将节点插入到第index位置之前
    template<class T>
    int DoubleLink<T>::insert(int index, T t) 
    {
        if (index == 0)
            return insert_first(t);
    
        DNode<T>* pindex = get_node(index);
        DNode<T>* pnode  = new DNode<T>(t, pindex->prev, pindex);
        pindex->prev->next = pnode;
        pindex->prev = pnode;
        count++;
    
        return 0;
    }
    
    // 将节点插入第一个节点处。
    template<class T>
    int DoubleLink<T>::insert_first(T t) 
    {
        DNode<T>* pnode  = new DNode<T>(t, phead, phead->next);
        phead->next->prev = pnode;
        phead->next = pnode;
        count++;
    
        return 0;
    }
    
    // 将节点追加到链表的末尾
    template<class T>
    int DoubleLink<T>::append_last(T t) 
    {
        DNode<T>* pnode = new DNode<T>(t, phead->prev, phead);
        phead->prev->next = pnode;
        phead->prev = pnode;
        count++;
    
        return 0;
    }
    
    // 删除index位置的节点
    template<class T>
    int DoubleLink<T>::del(int index) 
    {
        DNode<T>* pindex = get_node(index);
        pindex->next->prev = pindex->prev;
        pindex->prev->next = pindex->next;
        delete pindex;
        count--;
    
        return 0;
    }
    
    // 删除第一个节点
    template<class T>
    int DoubleLink<T>::delete_first() 
    {
        return del(0);
    }
    
    // 删除最后一个节点
    template<class T>
    int DoubleLink<T>::delete_last() 
    {
        return del(count-1);
    }
    
    #endif
  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/klsfct/p/10660075.html
Copyright © 2011-2022 走看看