栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++双链表简单操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++双链表简单操作

using namespace std;
typedef struct DNode{
    int data;
    struct DNode* prior,*next;
}DNode,*Dlinklist;
//建立双链表
void BuildDlinklist(Dlinklist &L){
    L =(Dlinklist)malloc(sizeof(DNode));
    L->next=NULL;
    L->prior=NULL;
    DNode *p = L;
    int x;
    cout<<"输入双链表的每一项,输入-1结束输入:";
    cin>>x;
    while (x!=-1) {
        DNode *s = new DNode;
        s->next = p->next;
        s->prior = p;
        p->next = s;
        s->data = x;
        p = s;
        cin>>x;
    }
}
//按序号查找
DNode *GetElmeById(Dlinklist L,int i){
    int j=1;
    DNode *p = L->next;
    if (i==0) {
        return L;
    }
    if (i<1) {
        return NULL;
    }
    while (p&&jnext;
        j++;
    }
    return p;
}
//插入数据
void InsertDlinklist(Dlinklist &L,int i,int x){
    DNode *p = GetElmeById(L, i-1);
    DNode *s = new DNode;
    s->next=p->next;
    p->next->prior=s;
    s->prior=p;
    p->next=s;
    s->data=x;
}
//按序号删除
void DelateById(Dlinklist &L,int i){
    DNode *p=GetElmeById(L, i-1);
    DNode *q=GetElmeById(L, i);
    p->next = q->next;
    q->next->prior = p;
    free(q);
}
//打印输出双链表
void PrintfDlinklist(Dlinklist L){
    DNode *p = L->next;
    while (p!=NULL) {
        cout<data<<" ";
        p=p->next;
    }
    cout< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号