题解 | #牛牛的单向链表#

牛牛的单向链表

https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4

#include "stdio.h"
typedef struct Node{
int data;
struct Node* next;

}node;
node* cai(int b){
    node* k=(node* )malloc(sizeof(node*));
    k->data=b;
    k->next=NULL;
    return k;
}
int main(){
    int n;
    scanf("%d",&n);
    node* head=NULL;
    node* tail=NULL;
    for(int i=0;i<n;i++){
        int v;
        scanf("%d",&v);
        node* p=cai(v);
        if(head==NULL){
            head=p;
            tail=p;
        }else{
            tail->next=p;
            tail=p;
        }

    }
    node* o=head;
    while(o!=NULL){
        printf("%d ",o->data);
        o=o->next;
    }
    o=head;
    while(o!=NULL){
        node* c=o;
        o=o->next;
        free(c);
    }

}

全部评论

相关推荐

程序员卤馆:加v细说
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务