数据结构实验之链表二:逆序建立链表

Description

输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据。

Input

第一行输入整数N;;
第二行依次输入N个整数,逆序建立单链表。

Output

依次输出单链表所存放的数据。

Sample

Input 

10
11 3 5 27 9 12 43 16 84 22 

Output 

22 84 16 43 12 9 27 5 3 11 
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct Node
 4 {
 5     int data;
 6     struct Node *next;
 7 }set;
 8 
 9 set *fun(set *head)//开结点并且连接好
10 {
11     set *node = (set *)malloc( sizeof(set));
12     node->next = head->next;//其实也就是NULL
13     head->next = node;
14     return node;
15 }
16 void Printf(set *head)//遍历输出
17 {
18     set *q;
19     q = head->next;
20     while(q)
21     {
22         printf("%d ",q->data);
23         q = q->next;
24     }
25 }
26 int main()
27 {
28     int i,n;
29     scanf("%d",&n);
30     set *head = (set*)malloc(sizeof(head));
31     head->next = NULL;
32     for(i=0;i<n;i++)
33     {
34         set *p = fun(head);
35         scanf("%d",&p->data);
36     }
37     Printf(head);
38     return 0;
39 }

 

posted @ 2020-06-07 12:37  爱写程序的机械师  阅读(260)  评论(0)    收藏  举报