//
// Created by 民 on 2021/9/26.
//
#include <iostream>

using namespace std;

typedef int Elem;

typedef struct LNode{
   
    Elem data;
    struct LNode* next;
}LNode,*LinkList;
//单链表
void Init_LinkList(LinkList &L){
   
    LNode *node = new LNode;//头结点
    node->data = 0;//存放表长
    node->next=NULL;
    L = node;
}
//获取长度
int Length(LinkList L){
   
    int len = 0;
    LNode *current = L->next;    //获取第一个节点
    while (current){
   
        len++;
        current=current->next;
    }
    return len;
}
//头插法
void head_insert_LinkList(LinkList &L){
   
    for (int i = 0; i < 10; ++i) {
   
        Elem e;
        LNode *node = new LNode ();
        cin>>e;
        node->data = e;
        node->next = L->next;
        L->next=node;
        L->data+=1;
    }
}
//尾插法
void tail_insert_LinkList(LinkList &L){
   
    LNode *tail=L;
    for (int i = 0; i < 10; ++i) {
   
        Elem e;
        LNode *node = new LNode ();
        cin>>e;
        node->data = e;
        node->next = NULL;
        tail->next = node;
        tail = node;
        L->data+=1;
    }
}
//输出链表
void print_LinkList(LinkList L){
   
    LNode *current = L->next;
    while (current){
   
        cout<<current->data<<" ";
        current = current->next;
    }
    cout<<endl;
}
//按序号查找节点
LNode *getElem(LinkList L,int i){
   
    int j=1;
    LNode *current = L->next;
    if (i==0)return L;
    if (i<0)return NULL;
    while (current&&j<i){
   
        current=current->next;
        j++;
    }
    return current;
}
//按值查找节点
LNode *LocateElem(LinkList L,Elem e){
   
    LNode *current = L->next;
    while(current && current->data!=e){
   
        current = current->next;
    }
    return current;
}
//插入指定位置-前插
void Insert_LinkList_head(LinkList &L,int pos,Elem e){
   
    LNode *node = new LNode;
    node->data = e;

    LNode *prior = getElem(L,pos-1);
    if (!prior)return;
    node->next = prior->next;
    prior->next = node;

}
//插入指定位置-前插
void Insert_LinkList_tail(LinkList &L,int pos,Elem e){
   
    LNode *node = new LNode;
    node->data = e;

    LNode *prior = getElem(L,pos);
    if (!prior)return;
    node->next = prior->next;
    prior->next = node;
}
void delete_LinkList(LinkList &L,int pos){
   
    LNode *prior = getElem(L,pos-1);
    if (!prior){
   return;}
// prior->next = prior->next->next;
    LNode *q = prior->next;
    prior->next = q->next;
    delete q;
}

int main(){
   
    LinkList L1,L2,L3;
    Init_LinkList(L1);
    Init_LinkList(L2);
    Init_LinkList(L3);
    head_insert_LinkList(L1);
    int len = Length(L1);
    cout<<"L1的长度:"<<len<<endl;
    Insert_LinkList_tail(L1,5,16);
    delete_LinkList(L1,2);
    print_LinkList(L1);


    tail_insert_LinkList(L2);
    len = Length(L2);
    cout<<"L2的长度:"<<len<<endl;
    Insert_LinkList_head(L2,5,16);
    delete_LinkList(L2,2);
    print_LinkList(L2);

}