题解 | #牛牛的链表添加节点#

牛牛的链表添加节点

https://www.nowcoder.com/practice/e33b79c2e15a41f9b541e73cd256124a

#include <stdio.h>
#include <stdlib.h>
typedef struct List{
    int data;
    struct List* next;
}List;

void fuzhi(List** L,int n,int arr[]){
    List* r=*L;
    List* p;
    for(int i=0;i<n;i++){
        p = (List*)malloc(sizeof(List));
        p->data= arr[i];
        p->next = NULL;
        r->next =p;
        r=p;
    }
}

void print(List* L){
    List* p =L;
    while(p->next){
        p=p->next;
        printf("%d ",p->data);
    }
}

void charu(List** L,int i){
    int j=0;
    List*p = *L;
    while(p->next&&j<i){
        p=p->next;
        j++;
    }
    List* s=(List*) malloc(sizeof(List));
    s->data=i;
    s->next = p->next;
    p->next =s;
}


int main(){
    int n,i;
    scanf("%d%d",&n,&i);
    int arr[n];
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    List* L=malloc(sizeof(List));
    L->next =NULL;
    fuzhi(&L,n,arr);
    charu(&L,i);

    print(L);
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
1
分享
牛客网
牛客企业服务