实验报告
系部 计算机系 课程名称— 实验名称 实验目的:
掌握栈的基本操作:入栈、出栈等在链式或顺序存储结构上的实现。
I班级I 数据结构
回文判断
I学号I I姓名 I实验日期
成绩
实验条件:PC机一台、VC++6.0编译环境
实验内容与算法思想: 内容:
输入一字符串判断其是否为回文。 算法思想:
1. 算法中主要用到的函数 ① void mai n() //主函数
② int push_seqstack(Seqstack *s,char x) // 进栈 ③ in t pop_seqstack(Seqstack *s) // 出栈 ④ int gettop_seqstack(Seqstack *s) 〃取栈顶元素 ⑤ int Ishuiwe n( char *s) 2. 函数之间的调用关系
函数之间的调用关系如图
1所示:
〃判断是否是回文
图1函数之间的调用关系
运行结果:
运行程序输入一段字符串,运行不同的结果如图
2、3所示:
图2
? F:不是回文的判断结果
嘗瞻结构就紳实蜀'回文请楓人子符串:1234asdfbFdSa4321 t 1234asdf bFdsa4321 呈 回文? Press any key to continue
图3 是回文的判断结果 实验总结:
通过前两次的实验,使我对
C语言和数据结构有许多认识。因此,这次实验
在做起来时多少有点思路,但是在实验室当中还是遇到了许多不会的问题,如回 文的判断思路、以及函数的调用等等,不过这些问题在老师和同学的帮助下基本 完成了这次实验。这次实验让我学到了很多知识,让我对原来所学的又重新复习 了一遍,同时还学到了许多其他新知识。
附:源程序: #in clude
#defi ne Maxsize 100 typedef struct { char elem[Maxsize];
int top; }Seqstack;
void init_seqstack(Seqstack *s) // 初始化 {
s->top=-1;
}
int push_seqstack(Seqstack *s,char x) // 进栈 {
if(s->top==Maxsize-1)
printf( return ERROR; }
栈已满 ********!\\n\
*** )
else
{ s->top++; s->elem[s->top]=x; return OK; }}
// 出栈
int pop_seqstack(Seqstack *s) { char x;
if(s->top==-1)
printf(
栈已空 ********** !\\n\
return ERROR;
else
{ x=s->elem[s->top]; s->top--; }
return x;
int gettop_seqstack(Seqstack *s) { char x;
//取栈顶元素
printf(\********** if(s->top==-1)
return ERROR; }
!\\n\栈已满 ***********
x=s->elem[s->top];
else
return x; }
//判断是否是回文
int Ishuiwen(char *s) {
Seqstack *STK,stack; char *p; int n; n=strlen(s); STK=&stack;
init_seqstack(STK); push_seqstack(STK,'#'); if(n%2==0) { p=s+n/2;
while(s!=p)
{ push_seqstack(STK,*s);
s++; }}
else
{ p=s+(n+1)/2;
while(s!=p-1)
{ push_seqstack(STK,*s);
s++;
}
while(STK->top!=-1 && gettop_seqstack(STK)!='#')
{ if(*p!=pop_seqstack(STK))
return ERROR; p++; }
return OK; }
void main() { char s[100];
printf(\请输入字符串: \gets(s); int x; x=Ishuiwen(s); if(x==1)
printf(\是回文 !\\n\else
printf(\不是回文 !\\n\
回文判断教学总结



