C语言 栈的一道习题

来源:百度知道 编辑:UC知道 时间:2024/09/23 00:33:31
假设称正读和反读都相同的字符序列为“回文”,例如,‘abba’和‘abcba’是回文,‘abcde’和‘ababab’则不是回文。试写一个算法判别读入的一个以‘@’为结束符的字符序列是否是“回文”。

请用栈来实现~ 用C语言写出来。。能运行,不要伪代码,谢谢

老实说,这题不用栈更简单,直接用两个下标变量,分别从头尾开始向中间遍历即可.

用栈代码如下

#include "stdio.h"

#define SIZE 100
char stack[SIZE];
int top;

void initstack()
{
top=0;
}

bool isempty()
{
return top==0;
}

bool isfull()
{
return top>=SIZE;
}

bool push(char x)
{
if(isfull()) return false;
stack[top++]=x;
return true;
}

bool pop(char &x)
{
if(isempty()) return false;
x=stack[--top];
return true;
}

main()
{
char str[81],c;
int i=0;
while((c=getchar())!='@')
{
str[i++]=c;
push(c);
}
i=0;
while(!isempty())
{
pop(c);
if(c!=str[i++])
{
printf("No");
return;
}
}
printf("Yes");
}

#include <iostream>
using namespace std;

c