zoukankan      html  css  js  c++  java
  • C++写的一个List<T>

    #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;
        int number ;
    public:
        List()
        {
            head=new Node<T>();
            head->next=NULL;
            head->pre=NULL;
            current=head;
            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;
            number++;
        }
        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;
                 }
            }
        }
        void Display()   
        {
                Node<T> * temp=current;
            while (temp->next!=NULL)
            {
                cout<<temp->value<<endl;
                temp=temp->next;
            }

        }
        int getNumber(){
            return number;
        }
    };

    void Test()
    {
        List<int> *p=new List<int>();
        for (int i=0;i<3553;i++)
        { p->Add(i);
        }

        p->Display();
        cout<<"个数:"<<p->getNumber()<<endl;
        getchar();
        cout<<p->Contains(333)<<endl;
        delete(p);
    }
        int main()
        {
            Test();
            getchar();
            return 0;
        }




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




    我的最近更新
    最新发布文章、框架、咨询等,来看看吧
  • 相关阅读:
    Ranorex发布2.3版本支持Flex4
    TestComplete基础教程
    2009年缺陷跟踪和测试管理工具使用情况调查报告
    软件自动化测试资源列表
    TestComplete资源列表
    分治算法
    画表格蓝桥杯
    分红酒蓝桥杯
    “硬币方案”蓝桥杯
    微生物增值蓝桥杯
  • 原文地址:https://www.cnblogs.com/humble/p/1790705.html
Copyright © 2011-2022 走看看