关于链栈求长度问题
#include <stdlib.h>#include <stdio.h>
typedef char ElemType;
typedef struct linknode
{
ElemType data;
struct linknode *next;
}LiStack;
typedef int Status;
#define ERROR 0
#define OK 1
#define STACK_INIT_SIZE
void InitStack(LiStack *S)
{
S=(LiStack *)malloc(sizeof(LiStack));
S->data=NULL;
S->next=NULL;
printf("Init successfully!\n");
}
void ClearStack(LiStack *S)
{
LiStack *p;
while(S->next!=NULL)
{
p=S;
S=S->next;
free(p);
}
free(S);
printf("OVER");
}
void StackLength(LiStack *S)
{
int i=0;
LiStack *q;
q=S;
while(q->next!=NULL)
{
q=q->next;
i++;
}
printf("The length is:%d\n",i);
}
void StackEmpty(LiStack *S)
{
if(S->next!=NULL)
printf("The stack is not empty!\n");
else
printf("The stack is empty!\n");
}
void Push(LiStack *S,ElemType e)
{
LiStack *p;
p=(LiStack *)malloc(sizeof(LiStack));
p->data=e;
p->next=S->next;
S->next=p;
printf("push %c successful!\n",e);
}
void Pop(LiStack *S,ElemType e)
{
LiStack *p;
if(S->next!=NULL)
{
p=S->next;
e=p->data;
printf("%c\n",e);
S=S->next;
}
}
void GetTop(LiStack *S,ElemType e)
{
if(S->next!=NULL)
e=S->next->data;
printf("The top elem is:%c\n",e);
}
void DispStack(LiStack *S)
{
LiStack *p;
p=S;
while(p->next!=NULL)
{
printf("\t%c\n",p->next->data);
p=p->next;
}
}
void main()
{
LiStack *S;
ElemType a='a',b='b',c='c',d='d',e='e';
S=(LiStack*)malloc(sizeof(LiStack));
InitStack(S);
StackEmpty(S);
Push(S,a);
Push(S,b);
Push(S,c);
Push(S,d);
Push(S,e);
StackEmpty(S);
StackLength(S);
DispStack(S);
StackEmpty(S);
ClearStack(S);
}
就是执行到红色的函数时,出现了问题,请各位高手给看看!!!