顺序栈定义练习

来源:互联网 发布:阿里巴巴一键铺货淘宝 编辑:程序博客网 时间:2024/04/30 16:02
#include<stdio.h>#include<stdlib.h>//顺序栈练习#define MaxSize 100#define ElemType inttypedef struct {ElemType data[MaxSize];int top;}SqStack;//top初始值为-1,top总是指向栈顶,top=max-1即为栈满,top=-1即为栈空,进top++,出top--//区分好栈指针和栈顶指针void Init(SqStack *&s){s = (SqStack *)malloc(sizeof(SqStack));s->top = -1;}void Delete(SqStack *&s){free(s);}bool Empty(SqStack *s){return (s->top == -1);}bool Push(SqStack *&s,ElemType e){if (s->top == MaxSize - 1)return false;s->top++;s->data[s->top] = e;return true;}bool Pop(SqStack *&s, ElemType &e){if (s->top == -1)return false;e = s->data[s->top];s->top--;return true;}//出栈,对栈进行改动bool GetTop(SqStack *s, ElemType &e){if (s->top == -1)return false;e = s->data[s->top];return true;}//只是获取栈顶元素值//判断回文练习bool pan(char str[]){int i; char e; SqStack *st;Init(st);for (i = 0; str[i] != '\0'; i++){Push(st, str[i]);}for (i = 1; str[i] != '\0'; i++){Pop(st,e);if (e != str[i]){return false;}}return true;}