编写一个程序,读入一行字符,且每个字符存入一个结点,按输入顺序建立一个链表的结点序列,然后再按相反顺序输出并释放全部结点。
函数接口定义:
在这里描述函数接口。例如:
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
struct Node *create()
{
struct Node *i;
struct Node *p;
char s;
i=NULL;
while((s=getchar())!='\n')
{
p=(struct Node *)malloc(sizeof(struct Node));
p->info=s;
p->link=i;
i=p;
}
return(i);
}
void print(struct Node *head)
{
struct Node *p;
p=head;
while(head!=NULL)
{
p=head;
head=p->link;
printf("%c",p->info);
free;
}
}