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

牛牛的单向链表

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

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct list
{
    int date;
    struct list* next;
}L;
L* SLTBuyNode(int x)
{
	L* newnode = (L*)malloc(sizeof(L));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	newnode->date = x;
	newnode->next = NULL;
	return newnode;
}
void insert(L** phead,int n)
{
     assert(phead);

     L* newnode = SLTBuyNode(n);
	if (*phead == NULL)
	{
		*phead = newnode;
	}
	else 
	{
		L* ptail = *phead;
		while (ptail->next)
		{
			ptail = ptail->next;
		}
		ptail->next = newnode;
	}
}
void print(L* phead)
{
    L* pcur = phead;
	while (pcur)
	{
		printf("%d ", pcur->date);
		pcur = pcur->next;
	}
}
int main()
{
    int x;
    scanf("%d",&x);
    L* head;
    head=NULL;
    for(int i=0;i<x;i++)
    {
      int n;
      scanf("%d",&n);
      insert(&head,n);
    }
    print(head);
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 15:27
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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