双向链表的封装

#include<iostream>
typedef  int DataType;
typedef  int Status;
typedef struct DNode
{   
    DataType data;
    struct DNode *Next;
    struct DNode *Prior;
}DNode,*DoubleList;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
using namespace std;
class D_List
{
    DoubleList L;
public:
    D_List()//???????;
    {
        L=(DoubleList)new DNode;
        L->Next=NULL;
        L->Prior=NULL;
    }
    void Creat_List_H()//???
    {   
        while(1)
        {
            DNode *p;
            bool b;
            p=(DNode *) new DNode;
            cout<<"???Data:"<<endl;
            cin>>p->data;
            p->Next=L->Next;
            p->Prior=L;
            L->Next=p;
            cout<<"Success"<<endl;
            cout<<"??????:   "<<endl;
            cout<<"?          1"<<endl;
            cout<<"?          0"<<endl;
            cin>>b;
            if(!b)
            {
                 break;
            }
        }
    }
    void Creat_List_T()//???
    {   
        
        DNode *tail;
        tail=(DNode *)L;
        while(1)
        {
            bool b;
            DNode *p;
            p=(DNode *) new DNode;
            cout<<"???Data:"<<endl;
            cin>>p->data;
            tail->Next=p;
            p->Prior=tail;
            tail=p;
            p->Next=NULL;
            cout<<"Success"<<endl;
            cout<<"??????:   "<<endl;
            cout<<"?          1"<<endl;
            cout<<"?          0"<<endl;
            cin>>b;
            if(!b)
            {
                 break;
            }
        }
    }
    DNode *GetNode()//??????????
    {  
        int  pos;
        DNode *p;
        cout<<"???????:"<<endl;
        cin>>pos;
        p=L->Next;
        pos--;
        while(p&&pos)
        {
            p=p->Next;
            pos--;
        }
        if(!p)
        {
            cout<<"????"<<endl;
        }
        return p;
    }
    DNode *Locate(DataType e)//????
    {   
        DNode *p;
        p=(DNode*)L->Next;
        while(p&&p->data!=e)
        {
            p=p->Next;
        }
        if(!p)
        {
            cout<<"????"<<endl;
        }
        else
            return p;
    }
    void InsList()
    {
        int pos;
        DNode *p,*q;
        cout<<"???????:"<<endl;
        cin>>pos;
        p=L->Next;
        pos--;
        while(p&&pos)
        {
                    p=p->Next;
                    pos--;
        }
        if(!p)
        {
            cout<<"????"<<endl;
            return;
        }
        else
        {  
            q=(DNode *)new DNode;
            cout<<"???DATA:"<<endl;
            cin>>q->data;
            if(!p->Next)
            {
                p->Next=q;
                q->Prior=p;
                q->Next=NULL;
            }
            else
                {    
                    q->Next=p;
                    q->Prior=p->Prior;
                    p->Prior->Next=q;
                    p->Prior=q;
                }
            cout<<"sucess"<<endl;
        }
    }
    void DelList()
    {
        int pos;
        DNode *p;
        cout<<"???????:"<<endl;
        cin>>pos;
        p=L->Next;
        pos--;
        while(p&&pos)
            {
                p=p->Next;
                pos--;
            }
        if(!p)
        {
            cout<<"????"<<endl;
            return;
        }
        else
        {  
            if(!(p->Next))
            {
                p->Prior->Next=NULL;
                delete(p);
            }
            else
            {
                p->Prior->Next=p->Next;
                p->Next->Prior=p->Prior;
                delete(p);
                cout<<"sucess"<<endl;
            }
        }
    }
    void Display()
    {      
        for(DNode *p=L->Next; p!=NULL; p=p->Next)
        {
            cout<<p->data<<endl;
        }
        cout<<"+++++++++++++++++++++++++++++"<<endl;  //????;
    }
    ~D_List()
    {
        DNode *p,*q;
        q=p=(DNode *)L->Next;
        while(q)
        {
            q=p->Next;
            delete(p);
            p=q;
        }
        delete(L);
        cout<<"??????"<<endl;
    }
};
int main()
{
    return 0;
}

原文地址:https://www.cnblogs.com/Howbin/p/8639413.html