前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >顺序栈检查回文

顺序栈检查回文

作者头像
别团等shy哥发育
发布2023-02-27 10:16:07
2140
发布2023-02-27 10:16:07
举报
文章被收录于专栏:全栈开发那些事

里面测试函数我是为了验证一个字符串是否是回文序列的,大家也可以把源文件改了,要用字符串就把数组改为字符数组,结构体的数据类型也可以改. 头文件:Stack.h

代码语言:javascript
复制
#define MaxSize 50
typedef struct{
   ElemType data[MaxSize];//存放栈中元素
   int  top;                          //栈顶指针
}SqStack;
//初始化
void InitStack(SqStack &S)
{
	S.top=-1;
}
//判空
bool StackEmpty(SqStack &S)
{
	if(S.top==-1)
		return true;
	else
		return false;
}
//进栈
bool Push(SqStack &S,ElemType x)
{
	if(S.top==MaxSize-1)     //栈空,报错
		return false;
 S.data[++S.top]=x;            //指针先加1,再入栈
 return true;
}
//出栈
bool Pop(SqStack &S,ElemType &x)
{
	if(S.top==-1)
		return false;
	x=S.data[S.top--];  //先出栈,指针再减一
	return true;
}
//读取栈顶元素
bool GetTop(SqStack &S,ElemType &x)
{
	if(S.top==-1)
	  return false;
	x=S.data[S.top];
	return true;
}
//比较两个数组的元素值是否相等
int Equal(int a[],int b[],int n)
{
	int flag=1;
   for(int i=0;i<n;i++)
   {
	   if(a[i]!=b[i])
	   {
		   flag=0;
		   break;
	   }
   }
   return flag;
}

源文件:

代码语言:javascript
复制
typedef int ElemType;
#include<stdio.h>
#include<malloc.h>
#include"Stack.h"
int main()
{
	int x;
	int a[5],b[5];
	SqStack mystack1;
    InitStack(mystack1);
	for(int i=0;i<5;i++);
	{
		scanf("%d",&a[i]);
	}
    for(i=0;i<5;i++)
	{
		Push(mystack1,a[i]);
	}
	for(int j=0; j<5;j++)
	{
       Pop(mystack1,b[j]);
	}
	for( j=0; j<5;j++)
	{
      printf("%d  ",b[j]);
	}
	x=Equal(a,b,5);
	if(x==1)
	{
		printf("这是一个回文");
	}
	else
	{
		printf("这不是回文!!");
	}
	return 0;
}

结果:

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档