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