求完善,在vc++上运行可以倒序输出一个字符串
#include<stdio.h>#include<stdlib.h>
typedef struct NODE
{
char data;
struct NODE *next;
}NODE;
NODE *push_stack(NODE *head);
void free_space(NODE *p);
int main()
{
NODE *head = NULL,*p = NULL;//链表都要有头结点 和头指针
p = push_stack(head);
while(p)
{
putchar(p->data);
p = p->next;
}
putchar(10);
free_space(p);
return 0;
}
NODE *push_stack(NODE *head)
{
char string;
NODE *stack = NULL;
while(1)
{
string = getchar();
if(string == '\0')
break;
stack = (NODE *)malloc(sizeof(struct NODE));//倒叙插入法 栈特性
stack->data = string;
stack->next = head;
head = stack;
}
return head;
}
void free_space(NODE *p)//直接递归释放就可以了
{
if(p->next)
free_space(p->next);
free(p);
}