题目:从键盘上输入一行字符,按输入时的逆序建立一个链表,即先输入的字符位于链表尾,然后按输入的相反顺序输出,并释放所有结点。
#include <string.h>
#include <stdlib.h>
#include<stdio.h>
struct node
{
char data;
struct node* next;
};
void main()
{
struct node*top ,* p;
char c = malloc(sizeof(char));
top = NULL;
while ((c = getchar()) != '\n')
{
p = (struct node*)malloc(sizeof(struct node));
p->data = c;
p->next = top;
top = p;
}
while (top)
{
p = top;
top = p->next;
putchar(p->data);
free(p);
}
}