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

posted @ 2010-08-02 18:38  [秦时明月]  阅读(590)  评论(0编辑  收藏  举报