反向输出结点

编写一个程序,读入一行字符,且每个字符存入一个结点,按输入顺序建立一个链表的结点序列,然后再按相反顺序输出并释放全部结点。

函数接口定义:

在这里描述函数接口。例如:
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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值