建立一个单链表,随机产生10个100以内的整数,代码已有,希望完善

建立一个单链表,随机产生10个100以内的整数,并按要求完成:
(1)在屏幕上显示单链表中的10个整数;
(2)删除值为a的结点,若不存在a,则把a插入到表尾,显示更新后的单链表;

以下代码未完成(2)希望完善


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
    struct Node{
        int Data;
        struct Node*Next;
    };
    void Print(struct Node *L){
        struct Node *q;
        q=L->Next;
        while(q!=NULL){
            printf("%d   ",q->Data);
            q=q->Next;
        }
    }

    void Insert(struct Node*L,int n){
        struct Node*p,*q;
        p=L;
        q=(struct Node*)malloc(sizeof(struct Node));

        while(p->Next!=NULL&&p->Next->Data<n){
            p=p->Next;
        }
        q->Data=n;
        q->Next=p->Next;
        p->Next=q;    
        
    }
    int main(){
        struct Node *L;
        L=(struct Node*)malloc(sizeof(struct Node));
        L->Next=NULL;
        srand((int)time(NULL));
        int i;
        for(i=0;i<10;i++){
    
            Insert(L,rand()%100); 
        }
        Print(L);
        return 0;
    }



你题目的解答代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
    struct Node{
        int Data;
        struct Node*Next;
    };
    void Print(struct Node *L){
        struct Node *q;
        q=L->Next;
        while(q!=NULL){
            printf("%d   ",q->Data);
            q=q->Next;
        }
    }

    void Insert(struct Node*L,int n){
        struct Node*p,*q;
        p=L;
        q=(struct Node*)malloc(sizeof(struct Node));

        while(p->Next!=NULL&&p->Next->Data<n){
            p=p->Next;
        }
        q->Data=n;
        q->Next=p->Next;
        p->Next=q;

    }
    void append(struct Node*L,int n){
        struct Node*p,*q;
        p=L;
        q=(struct Node*)malloc(sizeof(struct Node));
        while(p->Next!=NULL){
            p=p->Next;
        }
        q->Data=n;
        q->Next=NULL;
        p->Next=q;
    }
    void del(struct Node*L,int n){
        struct Node*p,*q;
        p=L->Next;
        q=L;
        while(p!=NULL){
            if (p->Data==n)
                q->Next = p->Next;
            else
                q = p;
            p=p->Next;
        }
    }
    struct Node* search(struct Node*L,int n){
        struct Node*p;
        p=L->Next;
        while(p!=NULL){
            if (p->Data==n)
                return p;
            p=p->Next;
        }
        return NULL;
    }


    int main(){
        struct Node *L;
        L=(struct Node*)malloc(sizeof(struct Node));
        L->Next=NULL;
        srand((int)time(NULL));
        int i;
        for(i=0;i<10;i++){

            Insert(L,rand()%100);
        }
        Print(L);
        printf("\n");
        int a;
        scanf("%d", &a);
        if (search(L,a))
            del(L,a);
        else
            append(L,a);
        Print(L);
        return 0;
    }

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632