Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include <string>
#include <stack>
#include <queue>

using namespace std;

class Stack
{
    Node*top;
    int count;

public:
    Stack();
    void push(char);
    char topGet();
    char pop();
    void display();
    bool isEmpty();
    int size();
};

class Queue
{
    Node *tail;
    Node *head;
    int count;
    char data;

public:
    Queue();
    void Dequeue();
    void Enqueue(char);
    bool isEmpty();
    bool isFull();
    void display();
    int size();
    char topGet();
};

class Node
{
    Node *next;
    char data;

public:
    Node();
    void setNext(Node *);
    void setData(char);
    Node* getNext();
    char getData();
};

bool isPallindrome(string);
int main()
{
    string input;
    bool flag = true;
    while (flag) {
        cout << "Enter word to see PALINDROME OR NOT, Press x for Quit" << endl;
        cout << "> ";
        getline(cin, input);
        if (input == "x" || input=="X")
        {
            flag = false;
            return 0;

        }

        if (isPallindrome(input))
        {
            cout << input << " is aPallindrome" << endl;
        }
        else
        {

            cout << input << " is not aPallindrome" << endl;
        }


    }
}

bool isPallindrome(string str)
{
    Stack *s;
    Queue *q;

    while (true) {
        s = new Stack();
        q = new Queue();
        for (int i = 0; i < str.length(); i++) {
            s->push(str[i]);
            q->Enqueue(str[i]);
        }
        bool eq = true;
        if (s->size() == q->size()) {
            while (!s->isEmpty()) {
                if (s->topGet() == q->topGet())
                {
                    eq = true;
                }
                else
                {
                    eq=false;
                }
                s->pop();
                q->Dequeue();

            }
        }
        else {
            eq = false;
        }
        return eq;
    }
}

Stack::Stack()
{
    top = NULL;
    count = 0;
}
void Stack::push(char s)
{
    Node*newNode = new Node();
    newNode->setData(s);
    newNode->setNext(top);
    top = newNode;
    count++;
}

bool Stack::isEmpty()
{
    if (count == 0)
        return true;
    return false;
}
char Stack::topGet()
{
    if (count == 0)
        throw "Stack is Empty ";
    char tempS;
    tempS = top->getData();
    return tempS;
}
char Stack::pop()
{

    if (isEmpty())
        return NULL;
    Node *temp;
    char tempS;
    tempS = top->getData();

    temp = top;
    top = top->getNext();
    delete temp;
    count--;
    return tempS;
}
void Stack::display()
{
    cout << topGet() << " ";
}
int Stack::size()
{
    return count;
}

Queue::Queue()
{
    head = new Node();
    tail = new Node;
    data = ' ';
    count = 0;
}

void Queue::Dequeue()
{
    Node *temp;
    if (isEmpty())
    {
        throw "There is Nothing in front to Queue ";
    }
    temp = head;
    data = temp->getData();
    head = head->getNext();
    temp = NULL;
    delete temp;
    count--;

}

void Queue::Enqueue(char val)
{
    Node *newNode = new Node();
    data = val;
    newNode->setData(val);
    newNode->setNext(NULL);
    if (head->getNext() == NULL)
    {
        head->setNext(newNode);
        head->setData(val);
        tail->setNext(newNode);
        tail = head;
    }
    else
    {
        tail->setData(val);
        tail->setNext(newNode);
        tail = newNode;
    }
    count++;
}

bool Queue::isEmpty()
{
    if (count == 0)
    {
        return true;
    }
    return false;

}
void Queue::display()
{
    cout << data << " ";
}
bool Queue::isFull()
{
    if (count == 10)
    {
        return true;
    }
    return false;
}
int Queue::size()
{
    return this->count;
}
char Queue::topGet()
{
    if (count == 0)
        throw "Stack is Empty ";
    return head->getData();
}

Node::Node()
{
    next = NULL;
    data = NULL;

}
void Node::setNext(Node*next)
{
    this->next = next;
}
void Node::setData(char data)
{
    this->data = data;
}
Node*Node::getNext()
{
    return this->next;
}
char Node::getData()
{
    return  this->data;
}


What I have tried:

i'm not sure where did i do wrong..its been for soo long i haven't touch c++
Posted
Updated 29-Jul-16 11:24am
Comments
jeron1 29-Jul-16 16:37pm    
Edit your post and list the errors. Is it compiler errors? runtime errors?
Member 12660833 30-Jul-16 1:39am    
the errors:
prog.cpp:11:3: error: unknown type name 'Node'
Node*top;
^
prog.cpp:26:3: error: unknown type name 'Node'
Node *tail;
^
prog.cpp:27:3: error: unknown type name 'Node'
Node *head;
^
prog.cpp:152:11: warning: implicit conversion of NULL constant to 'char' [-Wnull-conversion]
return NULL;
~~~~~~ ^~~~
'\0'
prog.cpp:253:10: warning: implicit conversion of NULL constant to 'char' [-Wnull-conversion]
data = NULL;
~ ^~~~
'\0'

compiled at https://ideone.com/7It4I4
Richard MacCutchan 30-Jul-16 5:31am    
You declare a Node* at the beginning of your Stack class, but you have never defined the Node type. You also seem to be over engineering what is a simple matter of copying and reversing a string and comparing it to the original. More thought about the process is needed before you start coding.

1 solution

[Update] In first, you forgot to tell that the errors where at compile time, so my advice don't match the problem. [/Update]

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

---------------------
Using stack and queue to check if palindrome is overly complicating things.
You should think about this:
“Everything should be made as simple as possible, but no simpler.” Albert Einstein

Try to check for palindrome by hand the simplest way, and see how you do. Do you need a stack or a queue ? I think not.
 
Share this answer
 
v3
Comments
Richard MacCutchan 30-Jul-16 5:28am    
This is hardly the correct response to a question where the code will not even compile.
Patrice T 30-Jul-16 7:48am    
The OP was not even able to tell if it was runtime error or compile time when he asked the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900