建立链表,并输出链表

 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  }

猜你喜欢

转载自www.cnblogs.com/The-Shining/p/11544484.html
今日推荐