头插法建立、输出单向链表的案例

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct obj
 4 {
 5     int num;
 6     struct obj *next;
 7 };
 8 int main(int argc, char *argv[])
 9 {
10     int n,i,t;
11     struct obj *head=NULL,*temp=NULL,*p=NULL;
12 
13     scanf("%d",&n);
14     for(i=0;i<n;i++)
15     {
16         scanf("%d",&t);
17         temp=(struct obj *)malloc(sizeof(struct obj));
18         temp->num=t;
19         temp->next=NULL;
20 
21         temp->next=head;
22         head=temp;
23     }
24 
25     p=head;
26     while(p!=NULL)
27     {
28         printf("%d ",p->num);
29         p=p->next;
30     }
31     printf("\n");
32     return 0;
33 }

 

posted on 2018-01-29 16:19  华山青竹  阅读(268)  评论(0编辑  收藏  举报

导航