//指针的进一步理解

#include "stdio.h"
#include "stdlib.h"
#define SIZE 4
struct student
{
    int num;
    struct student *next;
}stu[SIZE];

main()
{
    struct student *h=NULL,*q,*tail;
    int i=0;
    while(i<SIZE)
    {

      tail=(struct student *)malloc(sizeof(struct student));
     if(h==NULL)
          h=tail;//记录开始的值,输出时使用;但是不能中途赋值从中间开始输出,因为节点结构是一个整体
     else
         q->next=tail;
     q=tail;
     scanf("%d",&tail->num);
     tail->next=NULL;
     i++;
    }
    q=h;//需要一个头指针让q回到开头 q= ,否则q的值是最后输入的值,所以 q=q->next; 过后q=NULL 输出结束;
    while(q)
    {
        printf("%d ",q->num);
        q=q->next;
    }
}

猜你喜欢

转载自www.cnblogs.com/zby-yao/p/9242131.html