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

牛牛的链表添加节点

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

#include<stdio.h>
#include<stdlib.h>

struct List{
    int data;
    struct List *next;
};
void set_list(struct List *p,int n){
    int i=1;
    struct List *p1 = NULL;
    p1 = (struct List *)malloc(sizeof(struct List));
    p->next = p1;
    p1->next = NULL;
    scanf("%d",&(p1->data));
        while(n-1){
        set_list(p1, n-1);
        break;

    }
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    struct List *head = NULL;
    head = (struct List*)malloc(sizeof(struct List));
    set_list(head, n);
    struct List *p = head;
    for(int i=1;i<=m;i++){
        p=p->next;
    }
    struct List *newnode = (struct List*)malloc(sizeof(struct List));//申请新空间
    newnode->next = p->next;
    p->next = newnode;
    newnode->data = m;
    p=head->next;
    while(p){
        printf("%d ",p->data);
        p= p->next;
    }
    return 0;
}
全部评论

相关推荐

ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务