You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
984 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
//链表结构体一个真实的数字一个是指针指向下一个node的地址
struct node {
int data;
struct node *next;
};
int main() {
struct node *head ,*p,*q,*t;//第一个指针
int i, n, a; //变量声明
scanf("%d",&n); //创建一个多大的链表
head = NULL; //头指针初始为空
//循环读入n个数
for(i = 1; i <= n; i++) {
scanf("%d",&a); //读入一个整数放到a变量中
//动态申请一个空间用来存放一个节点并用临时指针p指向这个节点
p=(struct node *)malloc(sizeof(struct node));
p->data=a; //将数据存储到当前节点的data域中
p->next=NULL; //设置当前节点的后继指针指向空也就是当前节点的下一个节点为空。因为是最后一个节点所以next=NULL
//如果这是第一个创建的节点,则将上一个节点的后继指针指向当前节点
if(head==NULL)
head=p;
else
q->next=p; //如果不是第一个创建的节点,则将上一个节点的后继指针指向当前节点
//把本轮的变成上一个,方便下一轮循环
q=p;
}
//输出
t=head;
while(t!=NULL) {
printf("%d ",t->data);
t=t->next;
}
return 0;
}