完成回文判断函数的设计要求同时使用顺序栈和顺序循环队列两种数据结构并设计main函数进行测试给出测试结果不要用异或代码
回文判断函数的设计:
#include <iostream>
#include <string>
#include <stack>
#include <queue>
using namespace std;
// 使用顺序栈和顺序循环队列两种数据结构实现回文判断
bool isPalindrome(string str) {
stack<char> s;
queue<char> q;
int len = str.length();
for (int i = 0; i < len; i++) {
s.push(str[i]);
q.push(str[i]);
}
while (!s.empty() && !q.empty()) {
if (s.top() != q.front()) {
return false;
}
s.pop();
q.pop();
}
return true;
}
main函数进行测试:
int main() {
string str1 = "racecar";
string str2 = "hello";
cout << str1 << " is palindrome? " << isPalindrome(str1) << endl;
cout << str2 << " is palindrome? " << isPalindrome(str2) << endl;
return 0;
}
测试结果:
racecar is palindrome? 1
hello is palindrome? 0
``
原文地址: https://www.cveoy.top/t/topic/ckUb 著作权归作者所有。请勿转载和采集!