注册 登录
编程论坛 数据结构与算法

那位前辈讲讲这是为什么??实在想不明白

enenen 发布于 2011-07-09 20:20, 897 次点击
#include <stdlib.h>
#include <stdio.h>

typedef char ElemType;
typedef int Status;
#define ERROR 0
#define OK 1
#define STACK_INIT_SIZE

typedef struct linknode
{
ElemType data;
struct linknode *next;
}LiStack;

void InitStack(LiStack *S)
{
                                      //S=(LiStack *)malloc(sizeof(LiStack));
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);
}
   我就搞不懂了,为什么红色部分 //S=(LiStack *)malloc(sizeof(LiStack));不注释就会出错???
  麻烦那位前辈讲讲????
5 回复
#2
Amethystarry2011-07-23 23:55
你在调用InitStack(函数前已经为S申请空间了吧?
S=(LiStack*)malloc(sizeof(LiStack));
 InitStack(S);
 
既然已经有了
为什么还要申请两次
#3
世界模型2011-07-24 00:11
以下是引用Amethystarry在2011-7-23 23:55:52的发言:

你在调用InitStack(函数前已经为S申请空间了吧?
S=(LiStack*)malloc(sizeof(LiStack));
 InitStack(S);
  
既然已经有了
为什么还要申请两次
++楼上正解
#4
enenen2011-07-24 15:12
以下是引用enenen在2011-7-9 20:20:50的发言:

#include
#include

typedef char ElemType;
typedef int Status;
#define ERROR 0
#define OK 1
#define STACK_INIT_SIZE

typedef struct linknode
{
ElemType data;
struct linknode *next;
}LiStack;

void InitStack(LiStack *S)
{
                                      //S=(LiStack *)malloc(sizeof(LiStack));
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);
}
   我就搞不懂了,为什么红色部分 //S=(LiStack *)malloc(sizeof(LiStack));不注释就会出错???
  麻烦那位前辈讲讲????
   谢了
#5
enenen2011-07-24 15:13
回复 2楼 Amethystarry
谢了啊
#6
Alar302011-09-04 15:00
2#的是正解。。。
1