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;
}



全部评论

相关推荐

11-09 01:22
已编辑
东南大学 Java
高级特工穿山甲:羡慕,我秋招有家企业在茶馆组织线下面试,约我过去“喝茶详谈”😢结果我去了发现原来是人家喝茶我看着
点赞 评论 收藏
分享
有工作后先养猫:太好了,是超时空战警,我们有救了😋
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务