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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>

struct node
{
    char a;
    struct node *next;
};
int main()
{
        struct node *p,*q,*h,*t;
        int count=0,j;
        h = (struct node*)malloc(sizeof(struct node));
        p = (struct node*)malloc(sizeof(struct node));
        q = (struct node*)malloc(sizeof(struct node));
        h->next = p;
        char b[10];
        printf("请输入一行字符:\n");
        gets(b);
        for(int i=0;b[i]!='\0';i++)
        {

            p->a = b[i];
            p->next = q;//链接
            p = p->next;//p指向下一个
            q = (struct node*)malloc(sizeof(struct node));
            count++;
        }
        p->next = NULL;
        free(q);

        while(1)
        {
            j=1;//t指向第一个结点,所以从1开始计数
            t = h->next;
            while(j!=count)
            {
                j++;
                t = t->next;

            }
            count--;//逆序输出

            printf("%c",t->a);
            free(t);
            if (count ==0)
                break;
        }

        return 0;
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值