本程序中的链式栈不带头节点
#include "common.h"


void creat_cstack(cstacklink *cs)
{
*cs = NULL;
}
void clear_cstack(cstacklink *cs)
{
cstacklink p = NULL;
while(*cs)
{
p = *cs;
*cs = (*cs)->next;
free(p);
p = NULL;
}
}
int empty_cstack(cstacklink cs)
{
return cs == NULL ? TRUE : FALSE;
}
void push_cstack(cstacklink *cs, datatype x)
{
cstacklink q = (cstacklink)malloc(sizeof(cstack));
q->data = x;
q->next = NULL;

q->next = *cs;
*cs = q;
}
void pop_cstack(cstacklink *cs)
{
if(empty_cstack(*cs))
{
printf("cstack is empty!\n");
return;
}
cstacklink q = *cs;
*cs = (*cs)->next;
free(q);
q = NULL;
}
int get_top_cstack(cstacklink cs, datatype *ret)
{
if(empty_cstack(cs))
{
printf("cstack is empty!\n");
return FALSE;
}
*ret = cs->data;
return TRUE;
}
void display_cstack(cstacklink cs)
{
cstacklink p = cs;
while(p)
{
printf("%d,",p->data);
p = p->next;
}
putchar('\n');

}


//通过数组实现

#include "common.h"


void creat_sqstack(sqstacklink *sq)
{
*sq = (sqstacklink)malloc(sizeof(sqstack));
(*sq)->top = -1;
}
void clear_sqstack(sqstacklink sq)
{
sq->top = -1;
}
int empty_sqstack(sqstacklink sq)
{
return sq->top == -1 ? TRUE : FALSE;
}
int full_sqstack(sqstacklink sq)
{
return sq->top == MAXSIZE-1 ? TRUE : FALSE;
}
void push_sqstack(sqstacklink sq, datatype x)
{
if(full_sqstack(sq))
{
printf("stack is full!\n");
return;
}
sq->top++;
sq->data[sq->top] = x;
}
void pop_sqstack(sqstacklink sq)
{
if(empty_sqstack(sq))
{
printf("stack is empty!\n");
return;
}
sq->top--;
}
int get_top_sqstack(sqstacklink sq, datatype *ret)//取栈顶
{
if(empty_sqstack(sq))
{
printf("stack is empty!\n");
return FALSE;
}
*ret = sq->data[sq->top];
return TRUE;
}
void display_sqstack(sqstacklink sq)
{
int i=0;
while(i <= sq->top)
{
printf("%d,",sq->data[i]);
i++;
}
putchar('\n');
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值