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

牛牛的单向链表

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

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

//链表结点的定义
struct Node {
    int num;
    struct Node* Next;
};

//创建头结点
struct Node* creatList() {
    struct Node* headnode;
    headnode = (struct Node*)malloc(sizeof(struct Node));
    headnode->num = 0;
    headnode->Next = NULL;
    return headnode;
}

//创建结点
struct Node* creatnode(int num) {
    struct Node* newnode;
    newnode = (struct Node*)malloc(sizeof(struct Node));
    newnode->num = num;
    newnode->Next = NULL;
    return newnode;
}

int main() {
    int n, num;
    scanf("%d\n", &n);

    struct Node* L = creatList();
    struct Node* q = L;
    
    while (scanf("%d ", &num) != EOF) {
        struct Node* p = creatnode(num);
        
        //尾插法
        q->Next = p;
        q = p;
    }

    for (struct Node* p = L->Next; p != NULL ; p = p->Next) {
        printf("%d ", p->num);
    }
    return 0;
}

全部评论

相关推荐

强大的马里奥:我初中同学,没上高中,搞直播,现在提奔驰S450了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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