顺序栈及其基本运算实现——C语言

参考书:数据结构教程 第5版 李葆春 P80

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*顺序栈定义*/
#define MaxSize 100
typedef struct
{
    char data[MaxSize];
    int top;
}SqStack;
int j;

/*初始化栈*/
 void InitStack(SqStack *s){
    s->top=-1;
    s=(SqStack *)malloc(sizeof(SqStack));
}

/*销栈*/
void DestroyStack(SqStack *s){
    free(s);
}

/*判断栈是否为空*/
int StackEmpty(SqStack *s){
    return (s->top==-1);
}

/*进栈*/
int Push(SqStack *s, int e){
    if(s->top==MaxSize-1)
    {
        return 0;
    }
    s->top++;
    s->data[s->top]=e;
}

/*出栈*/
int Pop(SqStack *s, int *e){
    if(s->top==-1){
        return 0;
    }
    *e=s->data[s->top];
    s->top--;
}

/*取栈顶元素*/
int GetTop(SqStack *s,int *e){
    *e=s->data[s->top];
    if(s->top==-1){
        return 0;
    }
}

/*判断字符串是否为对称字符串*/
void symmetry(char str[]){
    int i;
    char e;
    SqStack st;
    InitStack(&st);
    for(i=0; str[i]!='\0'; i++){
        Push(&st, str[i]);
    }
    printf("%s\n",st.data);
    for(j=0; str[j]!='\0'; j++){
        printf("%c\n",str[j]);
        Pop(&st, &e);
        printf("%c-%c\n",e,str[j]);
        if(str[j]!=e){
            printf("非对称字符串\n");
            DestroyStack(&st);
            break;
        }
    }
    if (str[j]=='\0')
    {
        printf("字符串为对称字符串\n");
        DestroyStack(&st);
    }
}

int main(){
    char str[10]={"abczcba"};
    symmetry(str);
}
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值