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

牛牛的单向链表

https://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));
    head->next=NULL;
    head->data;
    return head;
}

void print(node* head)
{
    node *p=head->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}
node *add_newnode(node *head)
{
	node *newnode=(node *)malloc(sizeof(node));
	newnode->next=NULL;
	node *p=head;
	while(p->next!=NULL)
	p=p->next;
	p->next=newnode;
	return newnode;
}


int main() {
    node* head=add_head();
    int len;
    scanf("%d",&len);
    for(int i=0;i<len;i++)
    {
        int num;
        scanf("%d",&num);
        node *newnode=add_newnode(head);
        newnode->data=num;
    }
    print(head);
    
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
10-02 19:29
已编辑
浙江科技大学 运营
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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