回文字符的判断,同时运用两种数据结构进行巧妙判断

#include <iostream>
#define MaxSize 50
#include<string.h>

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

using namespace std;
typedef char ElemType;

typedef struct    //队列的结构
{
	ElemType data[MaxSize];
	int front ,rear;
} SqQueue;

//初始化队列
void InitQueue(SqQueue * &q)
{
	q=(SqQueue * )malloc(sizeof(SqQueue));
	q->front=q->rear=0;
}

//销毁队列
bool DestroyQueue(SqQueue * q )
{
	free(q);
	return true;
}

//判断队列是否为空的
bool QueueEmpty(SqQueue * q )
{
	return(q->front==q->rear);
}

//进队列
bool enQueue(SqQueue * &q, ElemType e  )
{
	if((q->rear+1)%MaxSize==q->front)
		return false ;
	q->rear=(q->rear+1)%MaxSize;
	q->data[q->rear]=e;
	return true;
}

//出队列
bool deQueue(SqQueue * &q ,ElemType &e )
{
	if (q->rear==q->front)
		return false ;
	q->front=(q->front+1)%MaxSize;
	e=q->data[q->front];
	return true;
}

typedef struct   //顺序栈的结构
{
	ElemType data[MaxSize];
	int top;
} SqStack ;

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

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

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

//进栈
bool Push(SqStack * & s ,ElemType e)
{
	if(s->top==MaxSize-1)
		return false ;
		s->top++;
		s->data[s->top]=e;
		return true;


}

//出栈
bool Pop(SqStack * & s ,ElemType & e )
{
	if(s->top==-1)
		return false;
	e=s->data[s->top];
	s->top--;
	return true ;
}

//取栈顶元素
bool GetTop(SqStack *s ,ElemType & e )
{
	if (s->top==-1)
		return false ;
	e=s->data[s->top];
	return true ;

}

int main()
{
    SqStack *s;
    SqQueue *q;
    ElemType e ,r;

    char a[50];

    int i,len;
    InitQueue(q);
    InitStack(s);
    printf("请你输入需要检验的回文字符:\n");
    scanf("%s",a);
    len=strlen(a);     //统计长度
    for(i=0;i<len;i++)
    {

    	Push(s,a[i]);
    	enQueue(q,a[i]);

    }


    bool p=true ;
    int j;
    while( j<len)
    {
    	deQueue(q,e);
    	Pop(s,r);
    	if(r==e)
	{
    	j++;
    	}
    	else
	{
	printf("这不是回文字符\n");
	p=false;
	break;
	}

    }
    DestroyQueue(q);
    DestroyStack(s);
    if(p)
	printf("这是回文字符\n");
	return 0;





}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值