数据结构栈操作

来源:互联网 发布:起点传奇数据库编辑器 编辑:程序博客网 时间:2024/06/05 23:26
#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;
}
原创粉丝点击