编辑代码

    #include <stdio.h>
    #include<stdbool.h>
    #include<stdlib.h>
    //单链表定义
    typedef struct Node
    {
        int data;
        struct Node* next;
    }Node,*LinkedList;
    //链表初始化
    bool InitList(LinkedList *L)
    {
        //头结点
        *L=(Node*)malloc(sizeof(Node));
        if(*L==NULL)
            return false;
        (*L)->next=NULL;
        return true;
    }
    //按照位序插入结点
    bool ListInsert(LinkedList *L,int i ,int e)
    {
        if(i<1)
            return false;
        //代插入结点
        Node* s = (Node*)malloc(sizeof(Node));
        if(s==NULL)
            return false;
        int j =0;
        //指向当前扫描的结点
        Node* p = *L;
        while(p!=NULL&&j<i-1)
        {
            p=p->next;
            j++;
        }
        if(p==NULL)
            return false;   //i值不合法
        //给代插入结点赋值,并将该结点连接到i-1结点的后面
        s->data=e;
        s->next=p->next;
        p->next=s;
        return true;
    }
    //指定结点后插入结点
    bool InsertNextNode(Node *p,int e)
    {   
        if(p==NULL)
            return false;
        Node* s = (Node*)malloc(sizeof(Node));
        if(s==NULL)
            return false;
        s->data=e;
        s->next=p->next;
        p->next=s;
        return true;
    }
    //指定结点前插操作
    bool InsertBeforeNode(Node *p,int e)
    {   
        if(p==NULL)
            return false;
        Node* s = (Node*)malloc(sizeof(Node));
        if(s==NULL)
            return false;
        s->next=p->next;
        p->next=s;
        //将待插入结点的前驱结点的值传给插入结点,覆盖待插入结点的前驱结点的值为e
        s->data=p->data;
        p->data=e;
        return true;
    }
    //按照位序删除
    bool ListDelete(LinkedList *L,int i , int *e)
    {
        if(i<1)
            return false;
        //移动指针,初始指向头节点
        Node* p =*L;
        int j=0;
        while(p!=NULL&&j<i-1)
        {
            p=p->next;
            j++;
        }
        //如果待删除结点的前驱结点或者本身为空,说明i错误
        if(p==NULL||p->next==NULL)
            return false;
        //s为代删除结点
        Node *s = p->next;
        *e=s->data;
        p->next=s->next;
        free(s);
        return true;
    }
    //删除指定结点
    bool DeleteNode(Node *p)
    {
        if(p==NULL)
            return false;
        if(p->next!=NULL)
        {
            Node* s=p->next;
            p->data=s->data;
            p->next=s->next;
            free(s);
        }
        //如果p->next==NULL,则不方便删除
    }
    //按位查找
    Node* GetElem(LinkedList L,int i)
    {
        Node* p = L;
        if(i<1)
            return NULL;
        int j=0;
        while(j<i&&p!=NULL)
        {
            p=p->next;
            j++;
        }
        return p;
    }
    //按值查找
    Node* LocateElem(LinkedList L,int e)
    {
        Node* p= L->next;
        while(p->data!=e&&p!=NULL)
            p=p->next;
        return p;
    }
    //求链表长度
    int GetLength(LinkedList L)
    {
        int j =0;
        Node* p=L->next;
        while(p!=NULL)
        {
            j++;
            p=p->next;
        }
        return j;
    }
    //尾插法建立单链表
    void ListTailInsert(LinkedList *L)
    {
        Node* s,*r=*L;
        int x;
        printf("请输入结点数据,-999结束:\n");
        do
        {   
            scanf("%d",&x);
            s=(Node*)malloc(sizeof(Node));
            s->data=x;
            r->next=s;
            r=s;
        }while(x!=-999);
        r->next=NULL;
    }
    void PrintList(LinkedList *L)
    {
        Node *p = (*L)->next;
        while(p!=NULL)
        {
            printf("%d ",p->data);
            p=p->next;
        }
        printf("\n");
    }
    //头插法建立单链表
    void ListHeadInsert(LinkedList *L)
    {
        Node* s;
        int x;
        printf("请输入结点数据,-999结束:\n");
        do
        {   
            scanf("%d",&x);
            s=(Node*)malloc(sizeof(Node));
            s->data=x;
            s->next=(*L)->next;
            (*L)->next=s;
        }while(x!=-999);
    }
    int main () {
        //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
        printf("Hello world!     - c.jsrun.net.");
        LinkedList L;
        if(!InitList(&L))
        {
            return -1;
        }
        ListHeadInsert(&L); 
        PrintList(&L);
        ListInsert(&L,4,4);
        PrintList(L);
        InsertNextNode(GetElem(L,3),5);
        PrintList(&L);
        int *e;
        ListDelete(&L,4,e);
        PrintList(&L);
        printf("%d",LocateElem(L,3)->data);
        return 0;
    }