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

牛牛的链表添加节点

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

#include<stdio.h>
#define INIT() (Node*)malloc(sizeof(Node))
typedef struct node{    //带头结点的链表
    int data;
    struct node* next;
}Node,*List;
List Init(){        //创建空链表,返回头节点
    Node* head=(Node*)malloc(sizeof(Node));
    head->data=0;
    head->next=NULL;
    return head;
}
void Insert(List head,int val){    //插入节点
    Node* cur=head;
    while(cur->next) cur=cur->next;
    Node* p=INIT();
    p->data=val;
    p->next=NULL;
    cur->next=p;
    head->data++;
}
void Add(List head,int i){
    Node *cur=head->next;
    int n=1;
    for(n;n<i;n++) cur=cur->next;
    Node* node=INIT();
    node->data=i; node->next=cur->next;
    cur->next=node;
}
int main(){
    int n,a,i;
    scanf("%d%d",&n,&i);
    List head=Init();
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        Insert(head,a);
    }
    Add(head,i);
    Node* cur=head->next;
    while(cur){
        printf("%d ",cur->data);
        cur=cur->next;
    }
    return 0;
}

没写出来不知道问题出在哪里了就是插入到后面

C语言刷题 文章被收录于专栏

自己从头开始刷的C语言

全部评论
你写复杂了大兄弟:void addNode(list &L,int i) { int t =i; list r=L; while(t--) { r=r->Next; } list s=new node; s->num=i; s->Next=r->Next; r->Next=s; }
点赞 回复 分享
发布于 2023-07-21 15:40 山东

相关推荐

程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
07-07 12:25
门头沟学院 Java
程序员牛肉:你这个智邮公司做的就是那个乐山市税务系统的服务吗?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务