2016年8月1日13:55:05
编写一个算法,判断任意给定的字符序列是否是回文序列,
什么是回文序列?
所谓回文是指一个字符序列顺着看和倒着看是完全相同的字符序列,例如12344321,12321
分析:可以通过构造栈和队列来实现,可先把任意给定的字符序列存放到队列和栈中,然后将字符序列出队和出栈,
比较出栈和出队列的字符是否相等,
1>若相同,则继续出栈,出队列,继续进行比较是否相等,直到堆栈和队列为空,
2>若不同,则说明该字符序列不是回文数,直接输出判断的结果,不是回文数;
3.在比较的过程中,如果堆栈出栈的字符和队列中出队的字符一直相同,此时堆栈和队列都为空时,
说明给点的字符序列是回文数
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef char DataType;
typedef struct node
{
DataType data;
struct node * pNext;
}linkList;
typedef struct stack
{
linkList * pTop;
linkList * pBottom;
}linkStack;
typedef struct
{
linkList * front;
linkList * rear;
}linkQueue;
void initLinkQueue(linkQueue * Q);
void traverseLinkQueue(linkQueue * Q);
int isEmpty(linkQueue * Q);
int enLinkQueue(linkQueue * Q,DataType element);
int deLinkQueue(linkQueue * Q,DataType * element);
int getHead(linkQueue * Q,DataType * element);
void clearLinkQueue(linkQueue * Q);
void initLinkStack(linkStack * S);
int isEmpty(linkStack * S);
int pushLinkStack(linkStack * S,DataType element);
int popLinkStack(linkStack * S,DataType * element);
int getTop(linkStack * S, DataType * element);
int getLength(linkStack * S);
void destroyLinkStack(linkStack * S);
void initLinkQueue(linkQueue * Q)
{
Q->front = (linkList * )malloc(sizeof(linkList));
if(!Q->front)
{
printf("动态内存分配失败!\n");
exit(-1);
}
Q->rear = Q->front;
Q->front->pNext = NULL;
return;
}
int isEmpty(linkQueue * Q)
{
if(NULL == Q->front->pNext)
return 1;
else
return 0;
}
int enLinkQueue(linkQueue * Q,DataType element)
{
linkList * pNew ;
pNew = (linkList * )malloc(sizeof(linkList));
if(!pNew)
{
printf("动态内存分配失败!\n");
exit(-1);
}
pNew->data = element;
pNew->pNext = NULL;
Q->rear->pNext = pNew;
Q->rear = pNew;
return 1;
}
int deLinkQueue(linkQueue * Q,DataType * element)
{
linkList * pFree;
if( isEmpty(Q) )
{
printf("队列为空,无法继续出队!\n");
return 0;
}
else
{
pFree = Q->front->pNext;
*element = pFree->data;
Q->front->pNext = pFree->pNext;
if(pFree == Q->rear)
{
Q->rear = Q->front;
}
free(pFree);
return 1;
}
}
int getHead(linkQueue * Q,DataType * element)
{
linkList * pHead;
if( isEmpty(Q) )
return 0;
pHead = Q->front->pNext;
*element = pHead->data;
return 1;
}
void traverseLinkQueue(linkQueue * Q)
{
linkList * pCur;
pCur = Q->front->pNext;
if(!pCur)
{
printf("队列为空!\n");
return;
}
while(pCur)
{
printf("%c",pCur->data);
pCur = pCur->pNext;
}
return ;
}
void clearLinkQueue(linkQueue * Q)
{
while(Q->front)
{
Q->rear = Q->front->pNext;
free(Q->front);
Q->front = Q->rear;
}
return;
}
void initLinkStack(linkStack * S)
{
S->pTop = (linkList * )malloc(sizeof(linkList));
if(!S->pTop)
{
printf("动态内存分配失败!\n");
exit(-1);
}
S->pBottom = S->pTop;
S->pBottom->pNext = NULL;
return ;
}
int isEmpty(linkStack * S)
{
if(S->pBottom == S->pTop)
return 1;
else
return 0;
}
int pushLinkStack(linkStack * S,DataType element)
{
linkList * pNew;
pNew = (linkList * )malloc(sizeof(linkList));
if(NULL == pNew )
{
printf("动态内存分配失败!\n");
exit(-1);
}
pNew->data = element;
pNew->pNext = S->pTop;
S->pTop = pNew;
return 1;
}
int popLinkStack(linkStack * S,DataType * element)
{
linkList * pFree;
if( isEmpty(S) )
{
printf("栈为空,无法继续出栈");
return 0;
}
pFree = S->pTop;
*element = pFree->data ;
S->pTop = S->pTop->pNext;
free(pFree);
return 1;
}
int getTop(linkStack * S, DataType * element)
{
if(isEmpty(S))
return 0;
*element = S->pTop->data ;
return 1;
}
int getLength(linkStack * S)
{
int i = 0;
linkList * pCur;
pCur = S->pTop;
while( pCur )
{
pCur = pCur->pNext;
i++;
}
return i;
}
void destroyLinkStack(linkStack * S)
{
while(S->pTop)
{
S->pBottom = S->pTop->pNext;
free(S->pTop);
S->pTop = S->pBottom;
}
return;
}
int main(void)
{
linkStack S;
linkQueue Q;
DataType ch;
DataType ch1;
initLinkStack(&S);
initLinkQueue(&Q);
printf("请输入一个字符序列:");
while('\n' != (ch = getchar()))
{
pushLinkStack(&S,ch);
enLinkQueue(&Q,ch);
}
printf("您所输入的字符序列:");
traverseLinkQueue(&Q);
printf("\n");
printf("出队序列 出栈序列\n");
while( !isEmpty(&S) )
{
deLinkQueue(&Q,&ch);
printf("%-10c",ch);
popLinkStack(&S,&ch1);
printf("%-4c\n",ch1);
if(ch1!=ch)
{
printf("该字符序列不是回文!\n");
return 0;
}
}
printf("该字符序列是回文!\n");
return 0;
}