Road to Coder _Stack_Palindrome

周次

学习时间

新编写代码行数

博客量(篇)

学到知识点

         

第15周

160

1

【数据结构】栈的回文实现

#include"stdio.h"
#include"stdafx.h"
#include"stdlib.h"
#include"windows.h"
#include"conio.h"
#include"string.h"


#pragma warning(disable 4996)

#define FALSE -1
#define TURE 0

#define Stack_Size 50                            //设栈中元素个数为50


typedef char StackElemType;


typedef struct
{
    StackElemType elem[Stack_Size];                //用来存储栈中元素的一维数组
    int top;                                //    用来存储栈顶元素的下标,top为 - 1表示空栈
}SeqStack;

void InitStack(SeqStack *S);                    //初始化顺序栈
void Push(SeqStack *S, StackElemType x);        //    进栈
void Pop(SeqStack *S, StackElemType *x);        //    出栈
void GetTop(SeqStack *S, StackElemType *x);        //获取栈顶元素
int SeqMatch(SeqStack *Mys, StackElemType *str);//回文判断
int IsEmpty(SeqStack *S);                        //判断是否的空栈

void main()
{
    SeqStack Mys;
    int result;
    StackElemType str[Stack_Size];                //一个用来判断是否是回文的字符串
    printf("请输入字符串:");
    gets_s(str);                                //输入
    result=SeqMatch(&Mys,str);                    //调用,接收返回值
    if (result)
    {
        printf("%s 是回文字符!
",str);
    }
    else
    {
        printf("%s 不是回文字符!
", str);
    }

    system("pause");
}


void InitStack(SeqStack *S)
{
    S->top = -1;
}

void Push(SeqStack *S, StackElemType x)
{

    if (S->top == 49)
    {
        printf("栈已满!");
        //return FALSE;
    }
    else
    {
        S->top++;
        S->elem[S->top] = x;
        //printf("%c	", S->elem[S->top]);
        //return TURE;
    }

}

void Pop(SeqStack *S, StackElemType *x)
{
    if (S->top == -1)
    {
        printf("空栈!");
        //return -1;
    }
    else
    {
        *x = S->elem[S->top];
        S->top--;
        //return (TURE);
    }
}

void GetTop(SeqStack *S, StackElemType *x)
{
    if (S->top == -1)
        printf("栈为空!");
        //return FALSE;
    else
    {
        *x = S->elem[S->top];
    }
}

int IsEmpty(SeqStack *S)
{
    if (S->top == -1)
    {
        printf("空栈!");
        return TURE;
    }
    else return FALSE;
}
/*    printf("%d",S->top);
    system("pause");
*/
int SeqMatch(SeqStack *Mys, StackElemType *str)                //str为mian函数输入的字符串
{
    char c = ' ';
//    char temp[Stack_Size];
    int flag = 0;                                            //判断&字符是否出现过
    int re = 1;                                                //用来判断 判断回文的过程中是否失败

    InitStack(Mys);

        
        //c = getchar();
        //Push(&Mys, c);
        int i,j;
        for (i = 0; flag==0 && str[i] != ''; i++)
        {
            c = str[i];                                        //将str里的每个字符拿出来判断使用
        //    temp[i] = c;
            switch (c)                
            {
            case'&': flag = 1; break;

            default:
                 Push(Mys, c);
            }
        }
        StackElemType topelem;                                //&后的字符,不入栈
        for (j = i; str[j]!=''; j++)
        {
            c = str[j];                                        //直接传值c,进行判断
            GetTop(Mys, &topelem);
            if (topelem == c)
            {
                Pop(Mys,&topelem);                            //成功一个,就删除栈顶元素继续匹配
            }
            else
            {
                re = 0;
                
                //printf("%s 不是回文字符!");
            }
        }
    

    return re;

}
原文地址:https://www.cnblogs.com/520-42/p/8803053.html