• List<T> C++实现


    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    template<typename T>
    struct Node
    {
        T value;
        Node* next;
        Node* pre;
    };
    template<typename T>
    class List
    {
    private:
        Node<T>* head;
        Node<T>* current;
        Node<T>* create;
        Node<T>*  read;
        int number ;
    public:
        List()
        {
            head=new Node<T>();
            head->next=NULL;
            head->pre=NULL;
            current=head;
             read=current;
            number=0;
        }
        ~List(){
            Node<T> * temp=current;
            while (temp->next!=NULL)
            {
                temp=temp->next;
                delete(temp->pre);
            }
            if (temp->next==NULL)
            {
                delete(temp);
            }
            cout<<"析构完成"<<endl;
        }
        void Add(T a)
        {
            create=new Node<T>();
            create->value=a;

            create->next=current;
            create->pre=NULL;
            current->pre=create;

            current=create;
            read=current;
            number++;
        }
        T getItem(int index){
            if(index>number-1){
                cout<<"异常"<<endl;
            }
            Node<T> * temp=current;
            for(int i=0;i<index;i++){
            temp=temp->next;
            }
            return temp->value;
        }
        bool Contains(T a){
            Node<T> * temp=current;
            bool ret=false;
            while (temp->next!=NULL)
            {
                if (temp->value==a)
                {
                    ret=true;
                    break;
                }
                temp=temp->next;
            }
            return ret;
        }
        void Remove(T a){
            Node<T> * temp=current;
            while (temp->next!=NULL)
            {
                 if (temp->value==a)
                 {
                     number--;
                    if (temp->pre!=NULL)
                    {
                        temp->pre->next=temp->next;
                        temp->next->pre=temp->pre;
                        delete(temp);
                    }
                    else
                    {
                        current=current->next;
                        delete(temp);
                    }
                    break;
                 }
                 else
                 {
                    temp=temp->next;
                 }
            }
            read=current;
        }
        void Display()   
        {
                Node<T> * temp=current;
            while (temp->next!=NULL)
            {
                cout<<temp->value<<endl;
                temp=temp->next;
            }

        }
        int getNumber()
        {
            return number;
        }
        Node<T>* Read()
        {
             if(read->next!=NULL){
                 read=read->next;
                 return read->pre;
             }
             else{
                return NULL;
             }
        }
    };

    void Test()
    {
        List<int> *p=new List<int>();
        for (int i=0;i<33;i++)
        { p->Add(i);
        }
        p->Display();
        cout<<"个数:"<<p->getNumber()<<endl;
        getchar();
        Node<int>* va=p->Read();
        while(va!=NULL){
            cout<<va->value<<endl;
            va=p->Read();
        }
        delete(p);
    }
        int main()
        {
            Test();
            getchar();
            return 0;
        }




    少侠,我看你气度不凡天赋异禀,这么帅,来了就给推荐一把吧




    我的最近更新
    最新发布文章、框架、咨询等,来看看吧
  • 相关阅读:
    实操记录之-----Ant Design of Vue 增强版动态合并单元格,自动根据数据进行合并,可自定义横纵向合并
    实操好用~~~~~antd 中 Table表格动态合并~~~
    超级容易理解的函数节流(throttle)
    Flask框架
    Celery框架
    redis数据库如何用Django框架缓存数据
    luffyapi项目 --短信认证的基本操作
    DRF之Jwt 实现自定义和DRF小组件及django-filter插件的使用
    Auth主件的(RBAC) 六表
    DRF之三大认证
  • 原文地址:https://www.cnblogs.com/humble/p/1790724.html
Copyright © 2020-2023  润新知