Question
To whose consider to answer this, please do not jump into different question or code than the one below. I got someone answered it but
To whose consider to answer this, please do not jump into different question or code than the one below. I got someone answered it but he/she didn't answered the right question. I really need your guys help so please answer to my question. Below is the code that I'm working on. The output should look similar like the below one. Thanks for your help so much.
PROBLEM: You are to write a program in C++ that solves the classic palindrome problem. A palindrome is a word or phrase that reads the same forwards as backwards. You must solve the palindrome problem using stacks and queues. Your program will read in a string, pushing each letter on a stack of characters and enqueueing each letter into a queue of characters. When the end of the string is reached, the program will then pop and dequeue the letters one at a time. If each of the letters is identical, then the string is the same backwards as forwards, and is therefore a palindrome.
You must write and get to work the six functions known as the stack Abstract Data Type (ADT) and the six functions known as the queue ADT. Use a class for each with an array to hold the elements. Using these functions, write a program that determines whether or not any given string is a palindrome.
The stack and queue approach may seem an overly complex solution, but this is aimed at familiarizing you with how stacks and queues work at a fundamental level.
Entered string: Output:
no eye on is a palindrome.
nonsense about palindromes is not a palindrome.
star rats is a palindrome.
Dennis sinned is a palindrome.
Able was I ere I saw Elba is a palindrome.
IMPLEMENTATION: You must have three program modules for this project. One should hold the Queue ADT, another the Stack ADT and the third should hold the main function and any other code for handling the palindromes. Make sure you make up "queue.h" and "stack.h" header files which should be #included at the top of your program with the other includes. Files with names such as "Queue.cpp" and "Stack.cpp" should hold the actual functions for those classes, whereas the .h files hold the class member functions, variables, and prototypes
==================================================
#include
#include
#include "Stack.h"
#include "Queue.h"
using namespace std;
void isPalindrome();
int main()
{
isPalindrome();
return 0;
}
void isPalindrome()
{
string palindrome;
char charS, charQ;
unsigned int n = 0;
bool valid = true;
Stack stack;
Queue queue;
stack.create();
queue.create();
cout << "Enter a string to determine whether or not it is a palindrome:";
getline(cin, palindrome);
cout << "You entered: " << palindrome << endl;
// This loop pushes and inserts the string into the stack and the queue
while(!stack.full() && !queue.full() && n < palindrome.length())
{
stack.push(palindrome[n]);
queue.insert(palindrome[n]);
n++;
}
// This loop pops and removes each character from the stack and queue
// to check if they are equal
while(!stack.empty() && !queue.empty())
{
stack.pop(charS);
queue.remove(charQ);
if (charS != charQ)
valid = false;
}
// This will print out whether or not the string is a palindrome
if (valid)
cout << palindrome << " is a palindrome." << endl;
else
cout << palindrome << " is not a palindrome." << endl;
stack.destroy();
queue.destroy();
}
#ifndef STACK_H_
#define STACK_H_
struct Snodes
{
char value;
struct Snode *next;
};
class Stack
{
public:
typedef struct Snodes snode;
typedef Snodes *SPTR;
void create();
void destroy();
bool empty();
bool full();
void push(char);
void pop(char&);
private:
SPTR top;
};
#endif /* STACK_H_ */
#ifndef QUEUE_H_
#define QUEUE_H_
struct Qnodes
{
char value;
struct Qnodes *next;
};
class Queue
{
public:
typedef struct Qnodes qnode;
typedef Qnodes *QPTR;
void create();
void destroy();
bool empty();
bool full();
void insert(char);
void remove(char&);
private:
QPTR front, rear;
};
#endif /* QUEUE_H_ */
#include
#include "Stack.h"
// Creates the stack by setting top equal to NULL
void Stack:: create()
{
top = NULL;
}
// Destroy the stack by popping off the contents until it is empty
void Stack::destroy()
{
char value;
while (!empty())
pop(value);
}
// Perform a boolean check to see if the stack is empty, which occurs if top is equal to NULL
bool Stack::empty()
{
return (top == NULL);
}
// Perform a boolean check to see if there is still space left in memory for more nodes
bool Stack::full()
{
SPTR temp;
temp = new Snodes;
if (temp == NULL)
return true;
else
{
delete temp;
return false;
}
delete temp;
}
// Pushes new items onto the stack
void Stack::push(char value)
{
SPTR temp;
temp = new Snodes;
temp->next = top;
temp->value = value;
top = temp;
}
// Pops items from the stack
void Stack::pop(char& value)
{
SPTR temp;
temp = top;
value = top->value;
top = top->next;
delete temp;
}
#include
#include "Queue.h"
// This creates a queue by setting both front and rear to NULL
void Queue::create()
{
front = rear = NULL;
}
// This destroys the queue by removing each item and discarding it until it is empty
void Queue::destroy()
{
char value;
while (!empty())
remove(value);
}
// Perform a boolean check to see if the queue is empty, which occurs if both front and rear are NULL
bool Queue::empty()
{
return (front == NULL && rear == NULL);
}
// Perform a boolean check to see if there is still space left in memory for more nodes
bool Queue::full()
{
QPTR temp;
temp = new Qnodes;
if (temp == NULL)
return true;
else
{
delete temp;
return false;
}
}
//
void Queue::insert(char value)
{
QPTR temp;
temp = new Qnodes;
temp->value = value;
if (empty())
{
front = rear = temp;
}
else
{
temp->next = front;
}
}
void Queue::remove(char& value)
{
QPTR temp;
if (front == rear)
{
temp = front;
value = front->value;
}
else
{
rear = rear->next;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started