编写一个程序,读入一行字符,且每个字符存入一个结点,按输入顺序建立一个链表的结点序列,然后再按相反顺序输出并释放全部结点。
函数接口定义:
在这里描述函数接口。例如:
struct Node *create();
void print(struct Node *head);
函数create,要求从表尾开始逆向建立链表;函数print,要求相反顺序输出并释放全部结点。
裁判测试程序样例:
解释
#include <stdio.h> #include <malloc.h> struct Node { char info; struct Node *link; }; struct Node *create(); void print(struct Node *head); int main() { struct Node *head; head=create(); print(head); return 0; } /* 你的代码将被嵌在这里 */
输入样例:
abcd
输出样例:
dcba
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
struct Node *create()
{
struct Node *top;
struct Node *p;
struct Node *head;
char s;
top=NULL;
while((s=getchar())!='\n')
{
p=(struct Node *)malloc(sizeof(struct Node));
p->info=s;
p->link=top;
top=p;
}
return(top);
}
void print(struct Node *head)
{
struct Node *p;
p=head;
while(head!=NULL)
{
p=head;
head=p->link;
printf("%c",p->info);
free;
}
printf("\n");
return 0;
}