本题要求实现,建立一个链表,按输入数据的逆序输出。
函数接口定义:
struct ListNode *create();
函数create利用scanf从输入中获取一系列正整数,当读到-1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。
链表节点结构定义如下:
struct ListNode {
int data;
struct ListNode *next;
};
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *create();
int main()
{
struct ListNode *p, *head = NULL;
head = create();
p = head;
while(p != NULL )
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
1 2 3 4 5 6 7 -1
输出样例:
在这里给出相应的输出。例如:
7 6 5 4 3 2 1
struct ListNode *create()
{
struct ListNode *p;
struct ListNode *o;
struct ListNode *i;
int a;
scanf("%d",&a);
o=NULL;
while(a!=(-1))
{
p=(struct ListNode *)malloc(sizeof(struct ListNode));
p->data=a;
p->next=o;
o=p;
scanf("%d",&a);
}
return(o);
}