求解啊,为什么链栈停不了啊,头疼啊
#include<stdio.h>#include<stdlib.h>
typedef struct ELEMENT
{
char ch;
}ELEMENT;
typedef struct NODE
{
ELEMENT data;
struct NODE *next;
}NODE;
void free_space(NODE *p);
int main()
{
ELEMENT string;
NODE *stack = NULL,*p = NULL;
while(1)
{
string.ch = getchar();
if(!string.ch)
break;
p = (NODE *)malloc(sizeof(struct NODE));
if(p == NULL)
{
printf("The space can not be assigned!\n");
return 0;
}
p->data = string;
p->next = stack;
stack = p;
}
while(p)
{
putchar(p->data.ch);
p = p->next;
}
putchar(10);
free_space(p);
return 0;
}
void free_space(NODE *p)
{
NODE *q;
while(q)
{
q = p->next;
free(p);
p = q;
}
free(q);
}