判断该字符序列是不是回文,3.31

头文件c1.h

#pragma once
#pragma once
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 10
#define STACKINCEEMENT 2
#define OVERFLOW -2
#define MAXQSIZE 10
#include <malloc.h>
#include<stdio.h>
#include<iostream>
typedef char  SElemType;
typedef char  QElemType;
typedef int Status;

源文件

#include"c1.h"

struct SqStack
{
	SElemType* base;
	SElemType* top;
	int stacksize;
};
struct SqQueue
{
	QElemType* base;
	int front;
	int rear;
};
Status InitStack(SqStack& S);
Status StackEmpty(SqStack S);
Status Push(SqStack& S, SElemType e);
Status Pop(SqStack& S, SElemType& e);
Status InitQueue(SqQueue& Q);
Status QueueEmpty(SqQueue& Q);
Status EnQueue(SqQueue& Q, QElemType d);
Status DeQueue(SqQueue& Q, QElemType& d);

Status judge()
{
	char c,a,b;
	SqStack S;
	SqQueue Q;
	InitStack(S);
	InitQueue(Q);
	while ((c = getchar() )!= '@')
	{
		Push(S, c);
		EnQueue(Q, c);
	}
	while (!StackEmpty(S))
	{
		Pop(S, a);
		DeQueue(Q, b);
		printf("%c和%c\n",a,b);
		if (a != b)
		{
			printf("该字符序列不是回文");
			return FALSE;
		}
		
		//printf("该字符序列是回文");
		
	}
	return OK;
}


int main()
{
	judge();

	system("pause");
	return 0;
}
Status InitStack(SqStack& S)
{
	if (!(S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType))))
		exit(OVERFLOW);
	S.top = S.base;
	S.stacksize = STACK_INIT_SIZE;
	return OK;
}
Status StackEmpty(SqStack S)
{
	if (S.top == S.base)
		return TURE;
	else
		return FALSE;
}
Status Push(SqStack& S, SElemType e)
{
	if (S.top - S.base >= S.stacksize)
	{
		S.base = (SElemType*)realloc(S.base, (S.stacksize + STACK_INIT_SIZE) * sizeof(SElemType));
		if(!S.base)
			exit(OVERFLOW);
		S.top = S.base + S.stacksize;
		S.stacksize += STACKINCEEMENT;
	}
	*(S.top)++ = e;
	return OK;
}
Status Pop(SqStack& S, SElemType &e)
{
	if (S.top == S.base)
		return ERROR;
	e = *(--S.top);
	return OK;
}
Status InitQueue(SqQueue& Q)
{
	Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
	if (!Q.base)
		exit(OVERFLOW);
	Q.front = Q.rear = 0;
	return OK;
}
Status QueueEmpty(SqQueue& Q)
{
	if (Q.front = Q.rear)
		return TURE;
	else
		return FALSE;
}
Status EnQueue(SqQueue& Q, QElemType d)
{
	if ((Q.rear + 1) % MAXQSIZE == Q.front)
		return ERROR;
	Q.base[Q.rear] = d;
	Q.rear = (Q.rear + 1) % MAXQSIZE;
	return OK;
}
Status DeQueue(SqQueue& Q, QElemType& d)
{
	if (Q.front == Q.rear)
		return ERROR;
	d = Q.base[Q.front];
	Q.front = (Q.front + 1) % MAXQSIZE;
	return OK;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二零二三.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值