#include<stdio.h>
//尾插法建立单链表
struct node{
int data;
struct node *next;
};
int main(){
int a,i,n;
struct node *head,*current,*p;
//head,current,p 均为指针型结构体
scanf("%d",&n);
head = NULL;
for(i = 0;i < n;i++){
scanf("%d",&a);
p = (struct node *)malloc(sizeof(struct node)); //p是一个结构体类型的指针
p -> data = a;
p -> next = NULL;
if(head == NULL){
head = p;
}else{
current -> next = p; //current代表地址里指的内容,current是一个指针,在此之前,
}
current = p;
}
struct node *t;
t = head; //两个都是指针的话可以直接赋值,不需要加上 & ,因为指针的名字就是地址(指针型变量)
do{
printf("%d ",t->data);
t = t -> next;
}while(t != NULL);
return 0;
}
网友评论