0
#include<iostream>  
#include<cctype>
#include<string>
#include<cstdlib>
#include"Palindrome.h"

using namespace std;

struct Node
{
  string pharse;
  Node* next;
};

int main()
{           
  Stack S1;
  Queue Q1;
  string word;
  int length;

  cout << "Do you know what a Palindrome is?\n";
  cout << "It is a word that is the same spelling backwards and forward\n";

  cout << "Enter in a word ";
  getline(cin, word);
  //cout.flush();
  length = word.length();
  string NewWord1[length];
  string NewWord2[length];
  for(int c =0; c < length; c++)
  {
    NewWord1[c] = word[c];
    NewWord2[c] = word[c];

  }
  for(int c =0; c < length; c++)
  {
    cout << NewWord1[c];
    cout << endl;

    cout << NewWord2[c];
    cout << endl;
  }


  cout << "end";

  S1.push(NewWord1, length);
  Q1.enqueue(NewWord2, length); 
  Node temp2 = S1.pop();
  Node temp3 = Q1.dequeue();
  if(temp2 == temp3)
    cout << "They are palindrome.";
  else
    cout << "They are not palindrome.";
  /*S1.pop();*/
  return 0;
}

void Stack :: push(string NewWord1[], int size)
{
        //cout << NewWord1[0];
        if(!isFull())   
        {
                for(int i = 0; i < size; i++)
                {
                        Node *temp = new Node;
                        temp -> pharse = NewWord1[i];
                        temp -> next = head1;
                        head1 = temp;
                }
        }
}   

// pop and enqueue return a Node which is my structure.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

3 Answers3

2

Since Nodes are objects, you must overload operator== for that class to take advantage of that syntax. Please show the class declaration for Node so we can help you implement that (it's probably as simple as comparing the data members for both nodes). Definition would be

bool Node::operator==(const Node & other) const
{
    return this->phrase == other.phrase;
}

See Vink's post as this is not the root of the problem. You need strings not arrays of strings, and you need to push the characters onto the stack.

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • There's usually a default comparison operator, though, isn't there? We'd need to see the `Node` class to really know what's going on – Cameron Apr 10 '11 at 22:28
  • Say a hypothetical node has 2 ints and a float contained in it. How would the compiler know how to compare nodes? – jonsca Apr 10 '11 at 22:30
  • I'd expect memberwise comparison, similar to the default `=` operator. I'm mistaken however, [there is no default](http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator) – Cameron Apr 10 '11 at 22:30
  • struct Node { string pharse; Node* next; }; – Cornelius Myles Apr 10 '11 at 22:31
  • @jonsca I thought about overloading but I did forget the process, and this really helped. Do you still call it like a regular function? – Cornelius Myles Apr 10 '11 at 22:40
  • You can, but now that you've overloaded it, you can just use `if(temp2 == temp3)` like you were doing. – jonsca Apr 10 '11 at 22:58
  • @jonsca: The compiler would apply the `==` operator recursively to the corresponding members of the class (the same way `=` operator works). However, there's no such feature in C++. – AnT stands with Russia Apr 10 '11 at 23:59
1

Does this even compile?

NewWord1 and NewWord2 are arrays of strings rather then strings.

Does the Node hold a character, string or array of strings?

Vink
  • 1,019
  • 2
  • 9
  • 18
  • It holds a string I am doing stacks and queues – Cornelius Myles Apr 10 '11 at 22:44
  • Yes, that's a good point. I think there are some inconsistencies of logic, as you really want to pop off the characters rather than the string. You'll need to modify what you are pushing, and you'll need a loop. – jonsca Apr 10 '11 at 23:01
  • I just added my push function in the code above because I keep getting Segmentation Fault Core dumped error. That could be why. – Cornelius Myles Apr 10 '11 at 23:08
  • @Cornelius Use a loop and push the characters of one string (not an array) on one at a time. There's no need for any of the nodes or anything (unless you want to make a node hold a char) – jonsca Apr 10 '11 at 23:13
  • @jonsca I do what nodes and I see what you mean. – Cornelius Myles Apr 10 '11 at 23:17
  • @Cornelius, yeah, you need to overhaul your stack and queue to take chars, and make NewWord1 and NewWord2 strings. – jonsca Apr 10 '11 at 23:21
  • @Jonsca so I need to replace the parameters in push to a char and should I change word, which is the variable that stores the word the user enters, to char. – Cornelius Myles Apr 10 '11 at 23:52
  • @Cornelius Yes, you should change word to a char. So pushing "hannah" would look like `['h']-['a']-['n']-['n']-['a']-['h']` on the stack (if you flip it vertically, lol) – jonsca Apr 10 '11 at 23:56
  • @Cornelius you might want to take a look at this efficient palindrome check implementation [here.](http://stackoverflow.com/questions/3285432/to-find-if-a-given-string-is-palindrome-or-is-not-palindrome) This is O(n) time complexity and makes only 2n passes of the chars in string. – Vink Apr 11 '11 at 07:17
0

My guess is that Node does not define bool operator==(Node const&) const.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113