码迷,mamicode.com
首页 > 其他好文 > 详细

建立链表,并输出链表

时间:2019-09-18 19:18:02      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:else   pre   struct   main   malloc   class   span   can   printf   

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct node  //结点
 4 {
 5     int data;
 6     struct node *next;
 7  };
 8  int main()
 9  {
10      struct node *head,*p,*q,*t;
11      int i,n,a;
12      scanf("%d",&n);
13      head=NULL;
14      for(i=0;i<n;i++){
15          p=(struct node *)malloc(sizeof(struct node));
16          scanf("%d",&a);
17          p->data=a;
18          p->next=NULL;
19          if(head==NULL)
20              head=p;
21          else
22              q->next=p;
23          q=p;
24      }
25      //输出链表
26      t=head;
27       while(t!=NULL){
28           printf("%d ",t->data);
29           t=t->next;
30       }
31       return 0;
32  }

 

建立链表,并输出链表

标签:else   pre   struct   main   malloc   class   span   can   printf   

原文地址:https://www.cnblogs.com/The-Shining/p/11544484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!