2021-09-27 循环双链表

//
// Created by 民 on 2021/9/27.
//
#include <iostream>
using namespace std;
typedef int Elem;
typedef struct DLNode{
    Elem data;
    struct DLNode *prior;
    struct DLNode *next;
}DLNode,*DLinkList;

//双链表
void Init_LinkList(DLinkList &L){
    DLNode *head = new DLNode;
    head->data=0;
    head->next = head;
    head->prior = head;
    L=head;
}
//获取长度
int Length(DLinkList L){
    cout<<"双链表获取长度:";
    DLNode *current = L;
    int len = 0;
    while (current->next!=L){
        len++;
        current=current->next;
    }
    cout<<len<<endl;
    return len;
}
//头插法
void head_insert_DLinkList(DLinkList &L){
    cout<<"头插法:插入十个数:"<<endl;
    for (int i = 0; i < 10; ++i) {
        DLNode *node = new DLNode;
        cin>>node->data;
        node->prior = L;
        node->next = L->next;
        L->next->prior = node;
        L->next = node;
        L->data++;
    }
}
//尾插法
void tail_insert_DLinkList(DLinkList &L){
    cout<<"尾插法:插入十个数:"<<endl;
    DLNode *tail = L->prior;
    for (int i = 0; i < 10; ++i) {
        DLNode *node = new DLNode ;
        cin>>node->data;
        node->next = L;
        node->prior = tail;
        L->prior = node;
        tail->next = node;
        tail=node ;
        L->data++;
    }
}
//输出链表
void print_DLinkList(DLinkList L){
    cout<<"输出双链表"<<endl;
    DLNode *current = L->next;
    while (current!=L){
        cout<<current->data<<" ";
        current=current->next;
    }
    cout<<endl;
}
//按序号查找节点
DLNode *getElem(DLinkList L,int i){
    cout<<"按序号查找节点,查询序号为"<<i<<"的节点:";
    DLNode *current = L->next;
    int j=1;
    if (i==0)return L;
    if (i<0)return NULL;
    while (current!=L && j<i){
        current = current->next;
        j++;
    }
    if (current){
        cout<<current->data<<endl;
    } else{
        cout<<"无此节点"<<endl;
    }
    return current;
}
//按值查找节点
DLNode *LocateElem(DLinkList L,Elem e){
    cout<<"按值查找节点,查询值为"<<e<<"的节点:";
    DLNode *current = L->next;
    while (current!=L && current->data!=e){
        current = current->next;
    }
    if (current){cout<<current->data<<endl;}
    else{cout<<"无此节点"<<endl;}
    return current;
}
//插入指定位置-前插
void Insert_DLinkList_head(DLinkList &L,int pos,Elem e){
    cout<<"在第"<<pos<<"个位置上前插:"<<e<<endl;
    DLNode *prior = getElem(L,pos-1);
    DLNode *node = new DLNode ;
    node->data = e;
    node->next = prior->next;
    node->prior = prior;
    prior->next->prior = node;
    prior->next = node;
    L->data++;
}
//插入指定位置-尾插
void Insert_DLinkList_tail(DLinkList &L,int pos,Elem e){
    cout<<"在第"<<pos<<"个位置上尾插:"<<e<<endl;
    DLNode *prior = getElem(L,pos);
    DLNode *node = new DLNode ;
    node->data = e;
    node->next=prior->next;
    node->prior=prior;
    prior->next->prior=node;
    prior->next=node;
    L->data++;
}
void delete_DLinkList(DLinkList &L,int pos){
    cout<<"删除第"<<pos<<"个位置的节点"<<endl;
    DLNode *q = getElem(L,pos);
    q->prior->next = q->next;
    q->next->prior = q->prior;
    delete q;
}
int main() {
    DLinkList L1,L2;
    Init_LinkList(L1);
    Init_LinkList(L2);

    head_insert_DLinkList(L1);
    print_DLinkList(L1);
    Length(L1);
    Insert_DLinkList_head(L1,5,17);
    print_DLinkList(L1);

    tail_insert_DLinkList(L2);
    print_DLinkList(L2);
    Length(L2);
    getElem(L2,5);
    LocateElem(L2,8);
    Insert_DLinkList_tail(L2,5,17);
    print_DLinkList(L2);
    delete_DLinkList(L2,2);
    print_DLinkList(L2);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值