因为痛,所以叫青春

我有一个梦想:穷屌丝变身富屌丝
逆序建链表

逆序建链表其实是一道很简单得程序题目,稍微动一下脑袋,就能找到门路,我们学习这个的时候只用了几分钟就把自己的链表建立起来了

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

 



posted on 2012-05-14 07:04  Nice!  阅读(265)  评论(0编辑  收藏  举报