數據結構實驗之鏈表二:逆序建立鏈表
Problem Description
Input
第二行依次輸入N個整數,逆序建立單鏈表。
Output
Example Input
10 11 3 5 27 9 12 43 16 84 22
Example Output
22 84 16 43 12 9 27 5 3 11
Hint
Author
參考代碼
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int n;
struct node *head,*p;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
scanf("%d",&n);
while(n--)
{
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = head->next;
head->next = p;
}
p = head->next;
printf("%d",p->data);
p = p->next;
while(p)
{
printf(" %d",p->data);
p = p->next;
}
printf("\n");
return 0;
}