数据结构算法--链表

来源:互联网 发布:thinkphp与php的区别 编辑:程序博客网 时间:2024/06/08 09:12
/**/////////////////////////////
//    //
//   链表数据结构  list.h //
//    //
/**///////////////////////////

#include<iostream.h>
template<class type>
class list;

template<class type>
class listnode
{
public:
    friend class list<type>;
private:
    type data;
    listnode<type> * next;
};

template<class type>
class list
{
public:
    list();
    ~list();
    void insertend(type); //向链表尾部插入元素
    bool insert(type,int); //向链表任意位置插入元素
    void delnode(int i);  //删除元素
    int find(type T);   //查找元素
    void makeempty();   //销毁链表
    bool print();  //打印链表
    int getlen();  //得到链表长度
private:
    listnode<type> *first,*last;
    int length;
};

template<class type>
void initlist(type &tmp);

template<class type>
void list_exit(list<type> &L,type tmp);

void initation();

template<class type>
void list_insertend(list<type> &L,type tmp);

template<class type> int list<type>::getlen()
{
    return length;
}

template<class type> void list<type>::makeempty()
{
    listnode<type> *p1,*p2;

    p1=first->next;
    first->next=NULL;
    while(p1!=NULL)
   {
        p2=p1;
        p1=p1->next;
        delete p2;
    }
    length=0;  
}

template<class type> void list<type>::insertend(type t)
{

    listnode<type> *p;
    p=new listnode<type>;
    p->data=t;
    p->next=NULL;
    last->next=p;
    last=p;

    length++;
}

template<class type> bool list<type>::insert(type t,int i)
{
    listnode<type> *p;
    p=first;

    int k=1;
    while(p!=NULL&&k<i)
    {
        p=p->next;
        k++;
    }
    if(p==NULL&&k!=i)
        return false;
    else
    {
        listnode<type> *tp;
        tp=new listnode<type>;
        tp->data=t;
        tp->next=p->next;
        p->next=tp;
        length++;

        return true;
    }
}

template<class type> void list<type>::delnode(int i)
{
    int k=1;
    listnode<type> *p,*t;
    p=first;

    while(p->next!=NULL&&k!=i)
    {
        p=p->next;
        k++;
    }
    t=p->next;
    cout<<"你已经将数据项 "<<t->data<<"删除"<<endl;

    p->next=p->next->next;
    length--;
    delete t;
}

template<class type> bool list<type>::print()
{
    listnode<type> *p=first->next;
    if(length==0)
        return false;
    else
    {
        cout<<"链表中有"<<length<<"项数据: "<<endl;
        while(p)
       {
            cout<<p->data<<" ";
            p=p->next;
        }
    }
    cout<<endl;


    return true;
}

template<class type> int list<type>::find(type T)
{
    listnode<type> *p=first->next;
    int i=1;
    while(p&&p->data!=T)
    {
        p=p->next;
        i++;
    }
    if(p)
        return i;
    else
        return 0;
}

template<class type> list<type>::~list()
{
    delete first;
    cout<<"欢迎再次使用 (!^!) "<<endl;
}

template<class type> list<type>::list()
{
    listnode<type> *node=new listnode<type>;
    node->next=NULL;
    first=last=node;
    length=0;
}

/**//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//    //
//   链表     List.cpp           //
//    //
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include"list.h"
#include<iostream.h>
#include<stdlib.h>

template<class type>
void initlist(type &tmp)
{
    list<type> List;
    int n;

    while(true)
    {

        cout<<"请选择你要对链表进行的操作 "<<endl
            <<"1) 在末尾插入数据"<<endl
            <<"2) 在任意处插入数据"<<endl
            <<"3) 删除数据项"<<endl
            <<"4) 删除整个链表"<<endl
            <<"5) 打印链表"<<endl
            <<"6) 查找数据项"<<endl
            <<"7) 退出"<<endl;

        cout<<"> ";
        cin>>n;

        while(n<1||n>7)
        {
            cout<<"输入有误,请从新输入!"<<endl;
            cout<<"> ";
            cin>>n;
        }

        switch(n)
        {
            case 1: list_insertend(List);break;
            case 2: list_insert(List);break;
            case 3: list_delnode(List);break;
            case 4: list_makeempty(List);break;
            case 5: list_print(List);break;
            case 6: list_find(List);break;
            case 7: return ;break;
        }
    }
}

void LIST()
{
    int n;
    cout<<"请选择你要构造的链表的数据类型 1)整型,2)字符型,3)浮点型"<<endl; 
    cout<<"> ";
    cin>>n;

    while(n<1||n>3)
    {
        cout<<"输入有误,请从新输入!"<<endl;
        cout<<"> ";
        cin>>n;
    }

    char t_c='c';
    int t_i=12;
    double t_f=23.3;

    switch(n)
    {
        case 1:initlist(t_i);break;
        case 2:initlist(t_c);break;
        case 3:initlist(t_f);break;
    }
}

template<class type>
void list_insertend(list<type> &L)
{
    type t;
    cout<<"请输入插入数据: >";
    cin>>t;
    L.insertend(t);
}

template<class type>
void list_find(list<type> &L)
{
    type T;
    cout<<"请输入你要查找的数据项:> ";
    cin>>T;

    int i;
    if(!(i=L.find(T)))
        cout<<"你要查找的数据项不存在!"<<endl;
    else
        cout<<"你要查找的数据项在第"<<i<<"个位置"<<endl;
}

template<class type>
void list_insert(list<type> &L)
{

    type t;
    cout<<"请输入插入数据: >";
    cin>>t;

    int n;
    cout<<"请输入插入位置: >";
    cin>>n;
    if(L.insert(t,n))
        cout<<"插入成功! 在"<<n<<"位置 插入"<<t<<endl;
    else
        cout<<"插入失败! 插入位置不正确!"<<endl;
}

template<class type>
void list_delnode(list<type>& L)
{
    int i;
    cout<<"请输入要删除数据项的位置: >";
    cin>>i;

    while(i<1||i>L.getlen())
   {
        cout<<"输入有误,可能大与链表长度,请从新输入!"<<endl;
        cout<<"> ";
        cin>>i;
    }
    L.delnode(i);
}
template<class type>
void list_makeempty(list<type> &L)
{
    L.makeempty();
}

template<class type>
void list_print(list<type> &L)
{
    if(!L.print())
        cout<<"链表为空!"<<endl;
}
0 0
原创粉丝点击