顺序栈

#include <stdlib.h>
#include <stdio.h>
//定义结构体
typedef struct{
    int *data;
    int capacity;
    int top;
}Stack;
//创建空栈
void Init(Stack *ps, int capacity){
    ps->capacity = capacity;
    ps->data = (int *)malloc(sizeof(int)*capacity);
    ps->top = -1;
}
//判断栈满
int IsFUll(Stack *ps){
    if(ps->capacity-1 == ps->top)
        return 1;
    else
        return 0;
}
//判断栈空
int IsEmpty(Stack *ps){
    if(ps->top == -1)
        return 1;
    else
        return 0;
}
//输出栈
void Print(Stack *ps){
    int i=0;
    if(IsEmpty(ps) == 0)
        for(i=0;i<=ps->top;i++)
            printf("%d\n",ps->data[i]);
}
//进栈
int Insert(Stack *ps, int x){
    if(IsFUll(ps))
        return -1;
    else{
        ps->data[++(ps->top)] = x;
        return 1;
    }
}
//弹栈
int Pop(Stack *ps, int *px){
    if(IsEmpty(ps))
        return -1;
    else{
        *px = ps->data[ps->top--];
        return 1;
    }
}
//栈顶
int Look(Stack *ps, int *px) {
    if (IsEmpty(ps))
        return -1;
    else {
        *px = ps->data[ps->top];
        return 1;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容