1.建立一个单链表,随机产生10个100以内的整数.

1.建立一个单链表,随机产生10个100以内的整数,并按要求完成:
(1)在屏幕上显示单链表中的10个整数;
(2)删除值为a的结点,若不存在a,则把a插入到第i个位置,显示更新后的单链表;
请在这个代码的基础上修改一下正确答案 谢谢!(此答案也是借鉴,侵权必删)
#include
#include
#include
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->Datap=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


代码正常运行哈