编辑代码

#include <iostream>
using namespace std;
typedef struct LNode{
    int *data;
    LNode *next;
}LNode, *Linklist;
//分配一个头结点
bool InitList(LNode &L){
    L=(LNode*)malloc(sizeof(LNode));
    if(L==NULL)
    {
        return false;
    }
    L->next=NULL;//头结点下一个指向为空
    return true;
}
//求链表长度
int length(Linklist L){
    int len=0;
    LNode* p=L;
    while(p->next!=NULL){
        p=p->next;
        len++;
    }
    return len;
}
//按序号查找结点
LNode* getelem(Linklist L,int i){
    LNode* p=L;
    int j=0;
    while(p!=NULL&&j<i){
        p=p->next;
        j++;
    }
    return p;
}
//按值查找
LNode* getelem(Linklist L,int e){
    LNode* p=L->next;
    while(p!=NULL&&p->data!=e){
        p=p->next;
    }
    return p;
}
//尾插
bool ListInsertnext(LNode* L,int e){
    if(p==NULL)
    return false;
    LNode* s=(int*)malloc(sizeof(LNode));//申请一块LNode
    if(s==NULL)
    return false;
    s->data=e;
    s->next=p->next;
    p->next=s;
    return true;
}
//插入
bool ListInsert(Linklist& L,int i,int e){
    LNode* p=L;
    if(i<1)
    return false;
    int j=0;
    while(p!=NULL&&j<i-1){
        p=p->next;
        j++;
    }
    return (ListInsertnext(p,e))
}
//前插
bool ListInsertPrim(LNode* L,int e){
    if(p==NULL)
    return false;
    LNode* s=(int*)malloc(sizeof(LNode));//申请一块LNode
    if(s==NULL)
    return false;
    s->next=p->next;
    p->next=s;
    s->data=p->data;
    p->data=e;
    return true;
}
//删除
bool ListDelete(Linklist& L,int i,int e){
    LNode* p=L;
    if(i<1)
    return false;
    int j=0;
    while(p!=NULL&&j<i-1){
        p=p->next;
        j++;
    }
    if(p==NULL)
    return false;
    if(p->next==NULL)//p结点之后已经无其他结点
    return false;
    LNode* q=p->next;
    e=q->data;
    p->next=q-next;
    free(q);
    return true;
}
//尾插法建立单链表
Linklist TailInsert(Linklist& L){
    int x;
    L=(LNode*)malloc(sizeof(LNode))
    LNode*s,*r=L;
    scanf("%d",&x);
    while(x!=9999){
        s=(LNode*)malloc(sizeof(LNode));
        s->data=x;
        r->next=s;
        r=s;
        scanf("%d",&x);
    }
    r->next=NULL;
    return L;
}
//头插法建立单链表
Linklist HeadInsert(Linklist& L){
    int x;
    L=(LNode*)malloc(sizeof(LNode))
    L->next=NULL;
    LNode*s;
    scanf("%d",&x);
    while(x!=9999){
        s=(LNode*)malloc(sizeof(LNode));
        s->data=x;
        s->next=L->next;
        L->next=s;
        scanf("%d",&x);
    }
    return L;
}
// 带头结点链表逆置
void ListReverse(Linklist& L){
    LNode* s;
    LNode *p;
    p=L->next;
    L->next=NULL;
    while(p){
        p=p->next;
        s=p;
        s->next=L->next;
        l->next=s;
    }
}
// 带头结点链表逆置
void ListReverse(Linklist& L){
    LNode* p=NULL;
    LNode* s=L->next;
    while(s){
        LNode* cur=s;
        s=s->next;
        cur->next=p;
        p=cur;
    }
}
int main() {
    Linklist L;
    InitList(L);
	return 0;
}