头文件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;
}