#include"SqQueue.h"
#include"SqStack.h"
//顺序栈判断是否是回文序列
bool PalindromeStack(SqStack*&S, int n)//S为顺序栈,n为栈中数组的长度
{
int e;
Pop(S, e);
int i;
int flag = 0;
for (i = 0; i < n / 2; i++)
{
if (S->data[i] == e)
{
Pop(S, e);
flag = 1;
}
else
{
return false;
}
}
if (flag == 1)
{
return true;
}
}
/*
int main(int argc, char* argv[])
{
SqStack* S;
InitStack(S);
Push(S, 1);
Push(S, 2);
Push(S, 3);
Push(S, 2);
Push(S, 1);
if (PalindromeStack(S, SqStackLength(S)))
{
printf("该序列为回文序列\n");
}
else
{
printf("该序列不是回文序列\n");
}
DestroyStack(S);
return 0;
}
*/
//循环队列判断是否是回文序列
bool PalindromeQueue(SqQueue*Q)
{
int a,b;
int c = Q->rear - 1;//c用作一个变量从后面往前遍历数据直至中间
for (int i = 0; i < QueueLength(Q) / 2; i++)
{
DeQueue(Q, a);//a用来存储前面的数据
b = Q->data[c];//b用来存储后面的数据
if (a == b)
{
c--;
}
else
{
return false;
}
}
return true;
}
/*
int main(int argc, char* argv[])
{
SqQueue* Q;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 1);
if (PalindromeQueue(Q))
{
printf("该序列为回文序列\n");
}
else
{
printf("该序列不是回文序列\n");
}
DestroyQueue(Q);
return 0;
}
*/
12-04
7920
05-31
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交