C ++ - 使用堆栈,以确定是否一个C风格的字符串是回文(C++ - Using a stack

2019-10-19 23:39发布

我正在上课的分配。 我们提供了这 3 类 。 (链接在这里,编辑等得翻转时,我试图做他们在不同的行)。

我们应该采取一个字符串,然后使用这些类来测试它是否是一个回文。 我们不能修改说类。

这是我的int main

#include <iostream>
#include <cstring>
#include <cctype>
#include "stack.h"

using namespace std;

int main () {
   char testString[100];
   Stack<char> charStack;

   cout << "Please enter a string:\n> ";
   cin.getline(testString, 100);

   char caps;
   for (int i = 0; i < strlen(testString); i++) {
      if (isalpha(testString[i])) {
         caps = toupper(testString[i]);
         charStack.push(caps);
      }
   }

   for (int j = 0; j < strlen(testString); j++) {
      if (isalpha(testString[j])) {
         caps = toupper(testString[j]);
         if (charStack.firstPtr->getData() == caps) { // This part is the issue. firstPtr is private in list.h, and I can't figure out another way to compare the char array to the data in the stack
            charStack.pop(caps);
         }
      }
   }

   if (charStack.isStackEmpty()) {
      cout << endl << endl << "\"" << testString << "\"  IS a palindrome." << endl;
   }
   else {
      cout << endl << endl << "\"" << testString << "\"  is NOT a palindrome." << endl;
   }
}

正如你所看到的,我不能完全弄清楚如何将字符数组,在弹出数据进行比较的数据。 Stack类只返回一个布尔值,以及指针ListNode对象类List是私有的,所以我不能使用该类的“的getData”功能! 任何人都可以帮助我任何提示?

谢谢!

Answer 1:

看看函数Stack::pop(STACKTYPE &data)

这需要在其中它存储要删除的元素(这实际发生在执行一非const引用参数List::removeFromFront(NODETYPE &value) )。

这意味着,你可以传递一个charpop()函数,之后它将包含你正在寻找的数据。



文章来源: C++ - Using a stack to determine if a C style string is a palindrome
标签: c++ stack