C++链表实现

博主分享了一段自己编写的C++链表实现代码,可供读者参考使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

吃饱了 又写了一个链表板子
#include <iostream>
#include <cassert>
#include <algorithm>
//needn't//#include <cstring>
//needn't//#include <cstdio>
//needn't//#include <iomanip>

using namespace std;

//==================enum==================
enum TAPE{Line,Blank};
//==============Template_Area==============
            template<class T>
    class node {
    public:
        node<T> *p_pre, *p_next;
        T data;
        node() { p_pre = p_next = NULL; }
        node(T aa) { p_pre = p_next = NULL; data = aa; }
    };
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
            template<class T>
    class XI {
    private:
        node<T> *head, *last;
    protected:
        int nodesum;
        static bool _comp(T a, T b) { return a < b; }
        node<T>* get_ad(int sub);
    public:
        //---------重载运算符---------
            T& operator[](int sub) {
                assert(sub >= 0 && sub < nodesum);
                //下标时越界!!====================
                return (get_ad(sub))->data;
            }
            friend ostream& operator<<(ostream& os,XI& xi) {
                xi.display(0, xi.nodenum(), Blank);
                return os;
            }
        //-------------end-------------

        //----------成员函数---------- 
            XI() { head = last = NULL; nodesum = 0; }
            virtual ~XI() { clear(); }
            int nodenum() { return nodesum; }
            bool none() { return nodesum == 0; }
            void add_node(node<T> *nd, int sub);
            void de_node(int sub);
            void clear();
            bool empty() { return nodesum == 0; }
            void display(int begin, int end, TAPE A = Line);
            void push(T a, int sub = 0) {
                node<T>* ls = new node<T>(a);
                add_node(ls, sub);
            }
        //-~-~-~-~-~-待测试-~-~-~-~-~-
                //#define DEBUG
                #ifdef DEBUG
                //快速排序
                #else
            void sort(int begin, int end, bool(*fun_comp)(T, T) = _comp) {
                node<T> *ls;//冒泡排序(要改动)
                for (int i = 1; i < nodesum; i++) {
                    ls = head->p_next;
                    for (int j = i; j < nodesum; j++) {
                        if (!(fun_comp(ls->p_pre->data, ls->data))) {
                            T tmp = ls->p_pre->data;
                            ls->p_pre->data = ls->data;
                            ls->data = tmp;
                        } ls = ls->p_next;
                    }
                }
            }
                #endif // <= DEBUG =>
        //-------------end-------------
    };
//==============Template_Area==============
//===============achievements===============
            template<class T>
    node<T>* XI<T>::get_ad(int sub) {
        node<T> *ls;
        int ts = nodesum - sub;
        if (ts >= sub) {
            ls = head;
            for (int i = 0; i < sub; i++)
                ls = ls->p_next;
        } else {
            ls = last;
            for (int i = 1; i < ts; i++)
                ls = ls->p_pre;
        } return ls;
    }
            template<class T>
    void XI<T>::add_node(node<T>* nd, int sub) {
        assert(sub >= 0 && sub <= nodesum);
        //添加节点时越界!!=================
        if (nodesum == 0) head = last = nd;
        else if (sub == 0) {
            nd->p_next = head;
            head->p_pre = nd;
            head = nd;
        } else if (sub == nodesum) {
            nd->p_pre = last;
            last->p_next = nd;
            last = nd;
        } else {
            node<T> *ls = get_ad(sub);
            ls->p_pre->p_next = nd;
            nd->p_pre = ls->p_pre;
            ls->p_pre = nd;
            nd->p_next = ls;
        } nodesum++;
    }
            template<class T>
    void XI<T>::de_node(int sub) {
        assert(sub >= 0 && sub < nodesum);
        //删除节点时越界!!================
        node<T> *ls;
        if (nodesum == 1) {
            delete head;
            head = last = NULL;
        } else if (sub == 0) {
            ls = head->p_next;
            delete head;
            head = ls;
            ls->p_pre = NULL;
        } else if (sub == nodesum - 1) {
            ls = last->p_pre;
            delete last;
            last = ls;
            ls->p_next = NULL;
        } else {
            ls = get_ad(sub);
            ls->p_next->p_pre = ls->p_pre;
            ls->p_pre->p_next = ls->p_next;
            delete ls;
        } nodesum--;
    }
            template<class T>
    void XI<T>::clear() {
        if (empty()) return;
        node<T> *de = head;
        while (head->p_next != NULL) {
            head = head->p_next;
            delete de;
            de = head;
        }delete de;
        nodesum = 0;
    }
            template<class T>
    void XI<T>::display(int begin, int end, TAPE A) {
        node<T> *ls = head;
        switch (A) {
        case Line:
            for (int i = begin; i < end; i++) {
                cout << ls->data << endl;
                ls = ls->p_next;
            } break;
        case Blank:
            for (int i = begin; i < end; i++) {
                cout << ls->data << " ";
                ls = ls->p_next;
            } cout << endl; break;
        }
    }
//===============achievements===============
你不嫌弃的话,随便扒去用吧.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值