单向链表的封装

#include<iostream>
typedef  int DataType;
typedef  int Status;
typedef struct Node
{   
    DataType data;
    struct Node *Next;
}Node,*LinkList;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
using namespace std;
class LList
{
    LinkList L;
public:
    LList()//构造一个空链表;
    {
        L=(LinkList)new Node;
        L->Next=NULL;
    }
    void Creat_List_H()//头插法
    {   
        while(1)
        {
            Node *p;
            bool b;
            p=(Node *) new Node;
            cout<<"请输入Data:"<<endl;
            cin>>p->data;
            p->Next=L->Next;
            L->Next=p;
            cout<<"Success"<<endl;
            cout<<"是否继续插入:   "<<endl;
            cout<<"是          1"<<endl;
            cout<<"否          0"<<endl;
            cin>>b;
            if(!b)
            {
                 break;
            }
        }
    }
    void Creat_List_T()//尾插法
    {   
        
        Node *tail;
        tail=(Node *)L;
        while(1)
        {
            bool b;
            Node *p;
            p=(Node *) new Node;
            cout<<"请输入Data:"<<endl;
            cin>>p->data;
            tail->Next=p;
            tail=p;
            p->Next=NULL;
            cout<<"Success"<<endl;
            cout<<"是否继续插入:   "<<endl;
            cout<<"是          1"<<endl;
            cout<<"否          0"<<endl;
            cin>>b;
            if(!b)
            {
                 break;
            }
        }
    }
    Node *GetNode()//按位置查找元素的地址
    {  
        int  pos;
        Node *p;
        cout<<"请输入查找位置:"<<endl;
        cin>>pos;
        p=L->Next;
        pos--;
        while(p&&pos)
        {
            p=p->Next;
            pos--;
        }
        if(!p)
        {
            cout<<"位置非法"<<endl;
        }
        return p;
    }
    Node *Locate(DataType e)//按值查找
    {   
        Node *p;
        p=(Node*)L->Next;
        while(p&&p->data!=e)
        {
            p=p->Next;
        }
        if(!p)
        {
            cout<<"输入非法"<<endl;
        }
        else
            return p;
    }
    void InsList()
    {
        int pos;
        Node *p,*q;
        cout<<"请输入插入位置:"<<endl;
        cin>>pos;
        pos--;
        if(pos==0)
        {
            p=L;
        }else{
                p=L->Next;
                pos--;
                while(p&&pos)
                {
                    p=p->Next;
                    pos--;
                }
            }
        if(!p)
        {
            cout<<"位置非法"<<endl;
            return;
        }
        else
        {  
            q=(Node *)new Node;
            cout<<"请输入DATA:"<<endl;
            cin>>q->data;
            q->Next=p->Next;
            p->Next=q;
            cout<<"sucess"<<endl;
        }
    }
    void DelList()
    {
        int pos;
        Node *p,*q;
        cout<<"请输入删除位置:"<<endl;
        cin>>pos;
        pos--;
        if(pos==0)
        {
            p=L;
        }else{
                p=L->Next;
                pos--;
                while(p&&pos)
                {
                    p=p->Next;
                    pos--;
                }
            }
        if(!p)
        {
            cout<<"位置非法"<<endl;
            return;
        }
        else
        {  
            q=p->Next;
            p->Next=q->Next;
            delete(q);
            cout<<"sucess"<<endl;
        }
    }
    void Display()
    {      
        for(Node *p=L->Next; p!=NULL; p=p->Next)
        {
            cout<<p->data<<endl;
        }
        cout<<"+++++++++++++++++++++++++++++"<<endl;  //分割输出;
    }
    ~LList()
    {
        Node *p,*q;
        p=(Node *)L->Next;
        while(p)
        {
            q=p;
            delete(q);
            p=p->Next;
        }
        delete(L);
        cout<<"此表已被销毁"<<endl;
    }
};
int main()
{
    return 0;
 }

posted @ 2018-03-24 16:05  Howbin  阅读(156)  评论(0编辑  收藏  举报