利用队列来判断回文数

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

//声名
#define MAXSIZE 100
#define OK 0
#define ERROR 1
#define OVERFLOW -1

typedef int Status;
typedef char QElemType;

//结构体
typedef struct
{
	QElemType* base;
	int front;
	int rear;
}SqQueue;

//初始化
Status InitQueue(SqQueue& Q)
{
	Q.base = new QElemType[MAXSIZE];
	Q.front = Q.rear = 0;
	return OK;
}

//入栈
Status EnQueue(SqQueue& Q, QElemType e)
{
	if ((Q.rear + 1 + MAXSIZE) % MAXSIZE == Q.front)
	{
		return ERROR;
	}
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1 + MAXSIZE) % MAXSIZE;
	return OK;
}

//出栈
Status DeQueue(SqQueue& Q, QElemType& e)
{
	if (Q.rear == Q.front)
		return ERROR;
	e = Q.base[Q.front];
	Q.front = (Q.front + 1 + MAXSIZE) % MAXSIZE;
	return OK;
}

//判断队列的长度
Status QueueLength(SqQueue& Q)
{
	return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}
//回文数判断函数
Status Inversion(char* str)
{
	int len = strlen(str);
	int i = 0;
	SqQueue Q;
	InitQueue(Q);
	for (i = 0; i < len / 2; i++)
	{
		EnQueue(Q, str[i]);
	}
	int j = len % 2 == 0 ? len / 2 : len / 2 + 1;
	QElemType e;
	while (Q.front - Q.rear != 0)
	{
		if (j = len / 2 + 1)
		{
			DeQueue(Q, e);
			if (e == str[++j])
				return 0;
			else
				return 1;
		}
		else
		{
			DeQueue(Q, e);
			if (e == str[j++])
				return 0;
			else
				return 1;
		}
	}
}

//主函数
int main()
{
	SqQueue Q;
	char  str[100] = { 0 };
	printf("请输入一串数字:\n");
	scanf("%s", &str);
	if (Inversion(str) == 0)
	{
		printf("是回文数\n");
	}
	else
	{
		printf("不是回文数\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值