题解 | #牛牛的单向链表#
牛牛的单向链表
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;
}
