欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

双向链表基本操作

程序员文章站 2024-03-22 11:12:52
...
#include<bits/stdc++.h>
using namespace std;
typedef struct  DulNode{
    int data;
    struct DulNode *prior,*next;
}DulNode,*DuLinkList;

bool InitDuList_L(DuLinkList &L){
    L=new DulNode;
    if(!L){
        return false;
    }
    L->prior=L->next=NULL;
    return true;
}

void CreateDuList_H(DuLinkList &L){
    int n;
    DuLinkList s;
    L=new DulNode;
    L->prior=L->next=NULL;
    cout<<"请输入元素个数n:"<<endl;
    cin>>n;
    cout<<"请依次输入每个元素,头插法"<<endl;
    while(n--){
        s=new DulNode;
        cin>>s->data;
        if(L->next){//已经有其他元素了
            L->next->prior=s;
        }
        s->next=L->next;
        s->prior=L;
        L->next=s;
    }
}
void Listprint_L(DuLinkList L){
    DuLinkList  p;
    p=L->next;
    while(p){
        cout<<p->data<<"\t";
        p=p->next;
    }
    cout<<endl;
}
bool GetElem_L(DuLinkList L,int i,int &e){
    int j;
    DuLinkList  p;
    p=L->next;
    j=1;
    while(j<i&&p){
        p=p->next;
        j++;
    }
    if(!p||j>i){
        return false;
    }
    e=p->data;
    return true;
}
bool LocateElem_L(DuLinkList L,int e){
    DuLinkList p;
    p=L->next;
    while(p&&p->data!=e){
        p=p->next;
    }
    //
    if(!p){
        return false;
    }
    return true;
}
bool ListInsert_L(DuLinkList &L,int i,int &e){
    int j;
    DuLinkList p,s;
    p=L;
    j=0;
    while(p&&j<i){
        p=p->next;
        j++;
    }
    if(!p||j>i){
        return false;
    }
    //j==i
    s=new DulNode;//生成新节点
    s->data=e;
    p->prior->next=s;
    s->prior=p->prior;
    s->next=p;
    p->prior=s;
    return true;
}
bool ListDelete_L(DuLinkList &L,int i){
    DuLinkList p;
    p=L;
    int j=0;
    while(p&&j<i){
        p=p->next;
        j++;
    }
    if(!p||j>i){
        return false;
    }
    if(p->next){
        p->next->prior=p->prior;
    }
    p->prior->next=p->next;
    delete p;
    return true;
}
int main(){
    int i,x,e,choose;
    DuLinkList L;
    choose=-1;
    while(choose!=0)
    {
        cout<<"1. 初始化\n";
        cout<<"2. 创建双向链表(前插法)\n";
        cout<<"3. 取值\n";
        cout<<"4. 查找\n";
        cout<<"5. 插入\n";
        cout<<"6. 删除\n";
        cout<<"7. 输出\n";
        cout<<"0. 退出\n";
        cout<<"请输入数字选择:";
        cin>>choose;
        switch (choose)
        {
            case 1: //初始化一个空的双向链表
                if(InitDuList_L(L))
                    cout<<"初始化一个空的双向链表!\n";
                break;
            case 2: //创建双向链表(前插法)
                CreateDuList_H(L);
                cout<<"前插法创建双向链表输出结果:\n";
                Listprint_L(L);
                break;
            case 3: //双向链表的按序号取值
                cout<<"请输入一个位置i用来取值:";
                cin>>i;
                if(GetElem_L(L,i,e))
                {
                    cout<<"查找成功\n";
                    cout<<"第"<<i<<"个元素是:"<<e<<endl;
                }
                else
                    cout<<"查找失败\n\n";
                break;
            case 4: //双向链表的按值查找
                cout<<"请输入所要查找元素x:";
                cin>>x;
                if (LocateElem_L(L,x))
                    cout<<"查找成功\n";
                else
                    cout<<"查找失败! "<<endl;
                break;
            case 5: //双向链表的插入
                cout<<"请输入插入的位置和元素(用空格隔开):";
                cin>>i;
                cin>>x;
                if(ListInsert_L(L, i, x))
                    cout<<"插入成功.\n\n";
                else
                    cout<<"插入失败!\n\n";
                break;
            case 6: //双向链表的删除
                cout<<"请输入所要删除的元素位置i:";
                cin>>i;
                if (ListDelete_L(L, i))
                    cout<<"删除成功!\n";
                else
                    cout<<"删除失败!\n";
                break;
            case 7: //双向链表的输出
                cout<<"当前双向链表的数据元素分别为:\n";
                Listprint_L(L);
                cout<<endl;
                break;
        }
    }
    return 0;
}