C语言 栈操作

#ifndef _STACK_H_
#define _STACH_H_


#define STACK_INIT_SIZE 100
#define STACK_ADD_SIZE 10


typedef  int stackData;

typedef struct {
    stackData *base;
    stackData *top;
    int         stacksize;  
}SqStack;


int InitStack(SqStack *S);
int DestroyStack(SqStack *S);
int ClearStack(SqStack *S);
int StackEmpty(SqStack *S);
int StackFull(SqStack *S);
int StackLength(SqStack *S);
int GetTop(SqStack *S , stackData *e);
int Pop(SqStack *S , stackData *e);
int Push(SqStack *S , stackData *e);
int PrintStack(SqStack *S);


#endif
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

int InitStack(SqStack *S)
{
    S->base = (stackData *)malloc(STACK_INIT_SIZE * sizeof(stackData));
    if(!S->base){
        printf("InitStack malloc fial");
        return -1;
    }
    S->top = S->base;
    S->stacksize = STACK_INIT_SIZE;
    return 0;
}
int DestroyStack(SqStack *S)
{
    free(S->base);
    S->base = NULL;
    S->top = NULL;
    S->stacksize = 0;
    return 0;
}
int ClearStack(SqStack *S)
{
    while(S->base != S->top){
        *(S->top --) = 0;
    }
}

int StackEmpty(SqStack *S)
{
    if(S->base == S->top)
        return 1;
    else
        return 0;
}
int StackFull(SqStack *S)
{
    if(S->stacksize <= (S->top - S->base))
        return 1;
    else
        return 0;
}
int StackLength(SqStack *S)
{
    return  S->top - S->base;
}

int GetTop(SqStack *S , stackData *e)
{
    if(S->top == S->base)
    {
        printf("GetTop stack is empty\n");
        return -1;
    }
    *e = *(S->top-1);
    return 0;
}

int Pop(SqStack *S , stackData *e)
{
    if(S->top == S->base)
    {
        printf("Pop stack is empty\n");
        return -1;
    }
    *e =  *(--(S->top));
    return 0;
}

int Push(SqStack *S , stackData *e)
{
    if((S->top - S->base) >= S->stacksize)
    {
        S->base = (stackData *)realloc(S->base , (S->stacksize + STACK_ADD_SIZE) * sizeof(stackData));
        if(!S->base)
        {
            printf("Push realloc fial ");
            return -1;
        }
        S->top = S->base + S->stacksize;
        S->stacksize = S->stacksize + STACK_ADD_SIZE;
    }
    *(S->top) = *e;
    (S->top)++;
    return 0;
}
int PrintStack(SqStack *S)
{
    if(StackEmpty(S))
    {
        printf("PrintStack StackEmpty\n ");
        return 0;
    }
    int x = S->top - S->base;
    int y = 1;
    printf("PrintStack:");
    for(; x >= y ; y++){
        printf("% d  ", *(S->top - y));
    }
    printf("\n");
    return 0;
}

int main(int argc , char *argv[])
{
    SqStack  stack;
    int n = 12;
    //stackData e,m;
    InitStack(&stack);

    //scanf("%d", n);
    printf("-------1------\n");
    Push(&stack , &n);
    printf("-------2------\n");
    n =13;
    Push(&stack , &n);
    n =13;
    Push(&stack , &n);
    n =13;
    Push(&stack , &n);
    PrintStack(&stack);
    printf("stacklenght:%d\n",StackLength(&stack));
    int m = 0;
    Pop(&stack , &m);
    printf("Pop :%d\n",m);
    printf("stacklenght:%d\n",StackLength(&stack));
    while(n)
    {
        m = n%8;
        printf("-------2------\n");
        Push(&stack , &m);
        n = n / 8;
    }

    printf("-------%d----%d--\n",StackEmpty(&stack),StackFull(&stack));
    PrintStack(&stack);
    /*while (!StackEmpty(stack))
    {
        Pop(stack ,&e);
        printf("%d",e);

    }*/

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值