用单链表模拟的堆栈……也许挫了点
这是结构体
struct letter_{
char save;
struct letter_ *last_ptr;
};
typedef struct letter_ stack;
//为新节点分配内存并把数据扔进去
int AddStack(stack *sptr,char data)
{
stack *temp = NULL;
if((temp=(stack*)malloc(sizeof(stack)))==NULL)
{
puts("Error to deliver space to the new element of the stack.");
return 0;
}
temp->save = data;
temp->last_ptr = sptr;
sptr = temp;
return 1;
}
//检查链表有多少个节点
int CheckStack(stack *sptr)
{
stack *t_sptr = sptr;
int number;
//for这里条件设置得有点复杂……凑合一下吧
for(number = 0;t_sptr->last_ptr!=NULL;number++)
{
#ifdef check
puts("For loop are doing.");
#endif
t_sptr = t_sptr->last_ptr;
}
return number;
}
然后运行时执行CheckStack总是返回0
是Add那里就已经写错了还是Check的判断出现了逻辑错误?
找了2天 木有找到 =。=
这是结构体
struct letter_{
char save;
struct letter_ *last_ptr;
};
typedef struct letter_ stack;
//为新节点分配内存并把数据扔进去
int AddStack(stack *sptr,char data)
{
stack *temp = NULL;
if((temp=(stack*)malloc(sizeof(stack)))==NULL)
{
puts("Error to deliver space to the new element of the stack.");
return 0;
}
temp->save = data;
temp->last_ptr = sptr;
sptr = temp;
return 1;
}
//检查链表有多少个节点
int CheckStack(stack *sptr)
{
stack *t_sptr = sptr;
int number;
//for这里条件设置得有点复杂……凑合一下吧
for(number = 0;t_sptr->last_ptr!=NULL;number++)
{
#ifdef check
puts("For loop are doing.");
#endif
t_sptr = t_sptr->last_ptr;
}
return number;
}
然后运行时执行CheckStack总是返回0
是Add那里就已经写错了还是Check的判断出现了逻辑错误?
找了2天 木有找到 =。=


