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

牛牛的单向链表

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

#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
	int data;
	Node* next;
}Node;
Node* head;
void insert(int x) {
	Node* temp = new Node();
	temp->data = x;
	temp->next = NULL;
	head = temp;
	Node* temp2 = head;
	while (temp2->next!= NULL)
	{
		temp2= temp2->next;
	}
	temp2->next = temp;
	temp->next=NULL;
}
void print() {
	Node* temp = head;
	while (temp!= NULL)
	{
		printf("%d ", temp->data);
		temp = temp->next;
	}

}
int main()
{
	int n;
	head = NULL;
	scanf("%d", &n);
	int i,x;
	for (i = 0; i < n; i++)
	{
		scanf("%d", &x);
		insert(x);
		print();
	}
	
	return 0;
}

全部评论

相关推荐

04-08 16:35
门头沟学院 Java
站队站对牛:实在是恶心的公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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