您的位置首页生活百科

读入的一个以@为结束符的字符序列是否为回文

读入的一个以@为结束符的字符序列是否为回文

的有关信息介绍如下:

读入的一个以@为结束符的字符序列是否为回文

试写一个算法,判断依次读入的一个以@为结束符的字符序列,是否为回文。所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”

头文件建立

#include "stdafx.h"

#include

using namespace std;

#define START 100;

#define LATER 10;

结构体的建立

typedef struct QNode

{

QElemType data;

struct QNode * next;

}QNode,*Queueptr;

typedef struct

{

Queueptr front;

Queueptr rear;

}LinkQueue;

typedef char SElemType;

typedef char QElemType;

typedef struct

{

SElemType *base;

SElemType *top;

int stacksize;

}stack;

栈和对列的基本操作

char push(stack&s,SElemType e)

{

if (s.top -s.base >=s.stacksize )

{

s.base =(SElemType*)realloc(s.base ,(s.stacksize +10)*sizeof(SElemType));

if(!s.base)exit(OVERFLOW);

s.top =s.base +s.stacksize ;

s.stacksize +=10;

}

*s.top ++=e;

return 1;

}

char pop(stack&s,SElemType&e)

{

if(s.top ==s.base )return 0;

e=*--s.top ;

cout<

return e;

}

char EnQueue(LinkQueue&q,QElemType e)

{

p = (Queueptr)malloc(100*sizeof(QNode));

if(!p)exit(OVERFLOW);

p->data=e;

p->next=p;

q.rear ->next =p;

q.rear =p;

return 1;

}

char DeQueue(LinkQueue&q,QElemType&e)

{

if(q.front ==q.rear )return 5;

p=q.front->next ;

e=p->data;

q.front ->next =p->next;

if(q.rear ==p)q.rear =q.front ;

cout<

free(p);

return e;

}

结果 测试

栈和对列的建立

char InitQueue(LinkQueue&q)

{

q.front =q.rear =(Queueptr)malloc(100*sizeof(QNode));

if(!q.front )exit(OVERFLOW);

q.front ->next= NULL;

return 1;

}

Queueptr p;

char InitStack(stack&s)

{

s.base =(SElemType*)malloc(100*sizeof(SElemType));

if(!s.base )exit(OVERFLOW);

s.top =s.base ;

s.stacksize =100;

return 1;

}

主函数的调用

int _tmain(int argc, _TCHAR* argv[])

{

int x=0;

QElemType f;

LinkQueue q;

SElemType a;

stack s;

InitStack(s);

InitQueue(q);

int b=0;

char c;

cout<<"请?输º?入¨?串ä?";

cin>>c;

while (c[b] != '@')

{

push(s,c[b]);

EnQueue(q, c[b]);

b++;

}

for (int i=0;i

{

char m=pop(s,a);

char n=DeQueue(q,f);

if(m!=n)

{cout<<"不?是º?回?文?"<

}

cout<<"是º?回?文?"<

return 0;

}