逆序链表建立和输出

#include <stdio.h>
#include <stdlib.h>

#define getnode(type) (type*)malloc(sizeof(type))

struct node
{
    char info;
    struct node*link;
}*top,*p;

char c;


int main()
{
    top = NULL;

    while( (c=getchar()) != '
')
    {
        p = getnode(struct node);
        p->info = c;
        p->link = top;    // 逆序建立链表
        top = p;          // 除了第一个节点  后面的节点都是报存上一个节点  也就是每建立一个节点 指向上一个节点
    }

    while(top)
    {
        p = top;
        top = top->link;   // abcdefg 
        putchar(p->info);  // gfedcba 
        free(p);
    }

    return 0;
}

 

 

 

一勤天下无难事。
原文地址:https://www.cnblogs.com/nowroot/p/15365984.html