List 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;
    }

原文链接: https://www.cnblogs.com/thinkingmore/archive/2010/08/02/1790724.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    List<T> C++实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/13320

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午12:41
下一篇 2023年2月7日 下午12:41

相关推荐