-1

In C++ programme, let's analyse stack data structure. My question is : Is it possible to check if the given stack contains palindrome or not WITHOUT ANY additional data structure and without modifying the stack? You are allowed to decompose the stack so long as you put it back together again (I mean that by modifying).

Example 1: s = {1, 2, 3, 2, 1} - it's palindrome Example 2: s = {1, 2, 3, 4, 5} - it's not a palindrome

  • My only idea is to do it with recursion:

void checkStack(Stack<int>& s){
    //Exit condition
    if(s.numberOfElements() == 0){
        return;
    } 

    int temp = s.pop(); 

    checkStack(s);

    s.push(temp); 
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/198269/discussion-on-question-by-kenan-saracevic-is-it-possible-to-check-if-word-stored). – Samuel Liew Aug 22 '19 at 00:19

2 Answers2

1

Is it possible to check if word stored on stack is palyndrome without ANY additonal data structures in C++

I think that you mean by data structures some other abstract data structures or containers like list, vector and so on.

You can do your task by enclosing in a wrapper the standard class std::stack<int>.

For example

#include <iostream>
#include <iomanip>
#include <stack>
#include <iterator>
#include <algorithm>

bool is_palindrome( const std::stack<int> &st )
{
    struct wrapper : std::stack<int>
    {
        wrapper( const std::stack<int> &st ) : std::stack<int>( st ) {}

        bool is_palindrome() const
        {
            return std::equal( std::begin( c ), std::next( std::begin( c ), c.size() / 2 ),
                               std::rbegin( c ) );
        }
    } w( st );


    return w.is_palindrome();
}

int main() 
{
    std::stack<int> st1( std::stack<int>::container_type{ 1, 2, 3, 2, 1  } );

    std::cout << std::boolalpha << is_palindrome( st1 ) << '\n';

    std::stack<int> st2( std::stack<int>::container_type{ 1, 2, 3, 4, 5  } );

    std::cout << std::boolalpha << is_palindrome( st2 ) << '\n';

    return 0;
}

The program output is

true
false

The class wrapper has an access to the protected data member c of the class std::stack<int> that denotes the underlying container.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

To be honest, I am pretty sure that you do not quote the task description fully correct. Several things are fishy about the task...

For one, one could do some hairsplitting and argue that it is impossible to use a std::stack without using any other data structure. A std::stack is just a container adapter that is build upon some underlying container. The default is a std::deque. Strictly speaking, using a std::stack without using any other data structure makes no sense.

Next, you are not allowed to modify the stack, but you are allowed to decompose it, as long as you put it back together again. This basically allows you to do anything. What I mean is, that you can for example write a function that returns you the nth element, eg:

int access(std::stack<int> s, size_t i) {
    while (i--) s.pop();
    return s.top();
}

Using this it is kind of trivial to write a test for palindrome. However this is basically treating the stack as if it wasn't a stack. For a similar approach that bypasses the stackyness of the std::stack see this answer.

Note that for the little cheat above I had to make a copy of the stack (s is passed by value) and I cannot imagine a solution, even a recursive one, that would not use at least a second stack. When you decompose the stack you need to store the elements somewhere, otherwise how would you restore it afterwards?

Let's forget about the exact task for a moment and consider how can we check if a std::stack contains a palindrome (without the unclear constraints), then one simple solution would be

bool check_palindrome(std::stack<int>& s) {
    std::stack<int> t;
    while (t.size() < s.size()) {
        auto temp = s.top();
        s.pop();
        t.push(temp);
    }
    if (t.size() > s.size()) t.pop();
    return s==t;
}

This could also be turned into a recursive method (to be honest I dont really get the hype about recursion), but I wouldn't know how to do it without the second stack. Also I didn't bother to reconstruct the original stack, but it would be easy to do so.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185