望远
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef char ElemType;
typedef struct linknode
{
ElemType data;
struct linknode *next;
}LiStack;
//初始化链栈
LiStack InitStack(LiStack *s)
{
s=(LiStack *)malloc(sizeof(LiStack));
s->next=NULL;
return *s;
}
//出栈
ElemType pop(LiStack *s,ElemType *e)
{
LiStack *p;
if(s->next==NULL)
{
return false;
}
p=s->next;
*e=p->data;
s->next=p->next;
free(p);
return *e;
}
//入栈
void push(LiStack *s,ElemType e)
{
LiStack *p=(LiStack *)malloc(sizeof(LiStack));
p->data=e;
p->next=s->next;
s->next=p;
}
//判断是否是回文序列
bool Match(LiStack *l,char str[],int length)
{
char c;
int i;
bool flag=true;
for(i=0;i<length;i++)
{
if(str[i]!=pop(l,&c))
{
flag=false;
break;
}
}
printf("\n");
return flag;
}
void main()
{
LiStack l;
int i=0,length;
char str[MAX],c;
l=InitStack(&l);
printf("请输入一行字符串以@结尾:\n");
scanf("%c",&c);
while(c!='@')
{
if(c!='\0'&&c!='\n')
{
str[i++]=c;
push(&l,c);
}
scanf("%c",&c);
}
str[i]='\0';
length=i/2;
if(Match(&l,str,length))
{
printf("是回文序列!!\n");
}
else
{
printf("不是回文序列!!\n");
}
}运行结果:刚好实验报告就是这个,不过是用c语言写的,哈哈~~