数据结构栈操作

#include<stdio.h>
#define N 10
typedef int datatype;
typedef struct
{
    datatype data[N];
    int top;
}sqstack;
sqstack *creat_empty_sqstack()
{
    sqstack *s;
    s=(sqstack *)malloc(sizeof(sqstack));
    s->top=-1;        //-1表示空栈
    return s;
}
int empty_stack(sqstack *s)
{
    return -1==s->top;
}
int full_stack(sqstack *s)
{
    return N-1==(*s).top;
}
void clear_stack(sqstack *s)
{
    if(empty_stack(s))
    {
        printf("此栈已是空栈");
    }
    else
    {
        s->top=-1;
        printf("清空成功!\n");
    }
}
int lenth_stack(sqstack *s)
{
    return s->top+1;
}

void push_stack(sqstack *s,datatype x)
{
    if(full_stack(s))
    {
        printf("此栈已满!\n");
    }
    else
    {
        s->top=s->top+1;
        if(N-1!=s->top)
        {
            s->data[s->top]=x;
            printf("插入成功:%d\n",s->data[s->top]);
        }
    }
}
datatype pop_sqstack(sqstack *s)
{
    if(empty_stack(s))
    {
        printf("空栈\n");
    }
    else
    {
        s->data[s->top]=0;
        s->top=s->top-1;
    }

}
datatype get_top(sqstack *s)
{
}
int main (int argc,char *argv[])
{
    in    
    sqstack *s;
    s=creat_empty_sqstack();
    printf("判空:%d\n",empty_stack(s));
    push_stack(s,200);
    push_stack(s,400);
    printf("lenth:%d\n",lenth_stack(s));
    pop_sqstack(s);
    printf("lenth:%d\n",lenth_stack(s));
    printf("判满:%d\n",full_stack(s));
    pop_sqstack(s);
    printf("判空:%d\n",empty_stack(s));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值