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

牛牛的单向链表

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

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

//单向循环链表
typedef struct node
{
    int data;
    struct node *next;
}node;

//创建头节点
node *add_head()
{
    node *Head = (node *)malloc(sizeof(node));
    if(Head == NULL)
        return NULL;
    Head->next = Head;
    return Head;
        
}

//尾插法
void add_node(node *Head,int data)
{
    node *new = (node*)malloc(sizeof(node));
    if(new == NULL)
        return;
    //节点成员赋值
    new->data = data;
    new->next = NULL;
    //链接
    
        node *pT = NULL;
        for(pT = Head;pT->next != Head;pT = pT->next);
        
        new->next = pT->next;
        pT->next = new;
}

//输出链表
void output(node *Head)
{
    if(Head->next == Head)
        return;
    node *pT = Head->next;
    while(pT != Head)
    {
        printf("%d ",pT->data);
        pT = pT->next;
    }
}

int main(void)
{
    node *Head = add_head();//头节点
    int n,i,j;
    scanf("%d",&n);
    int arr[n];
    
    //将键盘键入的数据存放到数组中
    for(i = 0;i < n;i++)
        scanf("%d",&arr[i]);
    
    //将数据插入链表
    for(j = 0;j < n;j++)
        add_node(Head, arr[j]);
    output(Head);
    
    return 0;
}

全部评论
兄弟,你这个好像不能编译通过。
2 回复 分享
发布于 2022-09-04 16:53 黑龙江

相关推荐

点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
24
3
分享

创作者周榜

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