栈的建立&操作(C语言)

#include "Stdio.h"
#define StackSize 100                                            /*定义储存空间大小*/
typedef char DataType;   

 

 

 /****************定义栈储存空间结构************/                                         

typedef struct
{
   DataType data[StackSize]; /**/
   int top; /**/

}SqlStack;

 

 

 

 /*****************生成栈*******************/
void Ininital(SqlStack* S)
{
     S->top=-1;
}

 

 

 

 /*****************判空****************/

int IsEmpty(SqlStack* S)
{
    return S->top==-1;
}

 

 

 /***************判满*****************/

int IsFull(SqlStack* S)
{
    return S->top==StackSize-1;
}

 

 

 /**************压栈*****************/

void Push(SqlStack* S,DataType ch)
{
    if(IsFull(S))
    {
        printf("full");
        exit(1);
    }
    S->data[++S->top]=ch;
}

 

 

 /***************弹桟**************/

DataType Pop(SqlStack* S)
{
    if(IsEmpty(S))
    {
        printf("empty");
        exit(1);
    }
    return S->data[S->top--];
}

 

 

 

 /****************取栈顶元素*****************/

DataType Top(SqlStack* S)
{
    if(IsEmpty(S))
    {
        printf("empty");
    }
    return S->data[S->top];
}

 

 

 

 /*************主函数******************/
int main(void)
{
    DataType x,y;
    SqlStack stack;

    Ininital(&stack);

    Push(&stack,'p');
    Push(&stack,'u');

    x=Top(&stack);
    Pop(&stack);

    y=Top(&stack);
    Pop(&stack);
    printf("%c",x);
    printf("%c",y);

    getch();
    return 0;
}

 

 

 

 

 

 

 

 

/*************另一种做法(类表现形式)*********************/

#include "Stdio.h"
#define StackSize 100
typedef char DataType;


typedef struct
{   DataType data[StackSize];
    int top;
    void (*Initial)();
    int (*IsEmpty)();
    int (*IsFull)();
    void (*Push)();
    char (*Pop)();
    char (*Top)();
}SqlStack;

 

void Initial(SqlStack *S)
{
    S->top=-1;
}

int IsEmpty(SqlStack *S)
{
    return S->top==-1;
}

int IsFull(SqlStack *S)
{
    return S->top==StackSize-1;
}

void Push(SqlStack *S,DataType x)
{  
    if(IsFull(S))
    {
        printf("stack is full ,can't input it");
        exit(1);
    }
    S->data[++S->top]=x;
}

DataType Pop(SqlStack *S)
{
    if(IsEmpty(S))
    {
        printf("the stack is empty ,can't output anything");
    }
    return S->data[S->top--];
}
DataType Top(SqlStack *S)
{
    if(IsEmpty(S))
    {
        printf("stack is null");
        exit(1);
    }
    return S->data[S->top];
}

void start(SqlStack* number)
{
    (*number).Initial=Initial;
    (*number).IsEmpty=IsEmpty;
    (*number).IsFull =IsFull ;
    (*number).Pop=Pop        ;
    (*number).Top=Top        ;
    (*number).Push=Push      ;
}
int main(void)
{
    SqlStack S;
    start(&S);

    S.Initial(&S);

    S.Push(&S,'p');
    S.Push(&S,'u');

    printf("%c",S.Top(&S));

    S.Pop(&S);
    printf("%c",S.Top(&S));

    getch();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值