Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I finished the program, but I need to make some changes. I need to to make the p

ID: 3706892 • Letter: I

Question

I finished the program, but I need to make some changes. I need to to make the program:

Prompts the user to type in a character string

?

Gets the characters and stores them in a stack data structure.

?

Makes use of the stack and a queue data structure to check for a palindrome.

?

Defines a function “isPalindrome” that determines whether the stringis a palindrome. The function“isPalindrome” returns 1 if x is a palindrome, and 0 ifx is not a palindrome. int isPalindrome(char *x)

?

Displays an appropriate message on the screen that indicates the string and whether the string is a palindrome or not.

Heres the link to the assingment if you have any questions:https://www.dropbox.com/s/mawyte4ivhadipw/CSC260_P5-Palindrone_StacksQueues.pdf?dl=0

CODE

======================

Stack.h

*******************************

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;

// Stack template
template <class T>
class Stack
{
public:
T *stackArray;
int stackSize;
int top;

//Constructor
Stack(int);

// Copy constructor
Stack(const Stack&);

// Destructor
~Stack();

// Stack operations
void push(T);
T pop();
bool isFull();
bool isEmpty();
};

//***************************************************
// Constructor *
//***************************************************

template <class T>
Stack<T>::Stack(int size)
{
stackArray = new T[size];
stackSize = size;
top = -1;
}

//***************************************************
// Copy constructor *
//***************************************************

template <class T>
Stack<T>::Stack(const Stack &obj)
{
// Create the stack array.
if (obj.stackSize > 0)
stackArray = new T[obj.stackSize];
else
stackArray = NULL;

// Copy the stackSize attribute.
stackSize = obj.stackSize;

// Copy the stack contents.
for (int count = 0; count < stackSize; count++)
stackArray[count] = obj.stackArray[count];

// Set the top of the stack.
top = obj.top;
}

//***************************************************
// Destructor *
//***************************************************

template <class T>
Stack<T>::~Stack()
{
if (stackSize > 0)
delete [] stackArray;
}

//*************************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************************

template <class T>
void Stack<T>::push(T item)
{
if (isFull())
{
cout << "The stack is full. ";
}
else
{
top++;
stackArray[top] = item;
}
}

//*************************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//*************************************************************

template <class T>
T Stack<T>::pop()
{
if (isEmpty())
{
cout << "The stack is empty. ";
return '';
}
else
{
T item = stackArray[top];
top--;
return item;
}
}

//*************************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//*************************************************************

template <class T>
bool Stack<T>::isFull()
{
bool status;

if (top == stackSize - 1)
status = true;
else
status = false;

return status;
}

//*************************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//*************************************************************

template <class T>
bool Stack<T>::isEmpty()
{
bool status;

if (top == -1)
status = true;
else
status = false;

return status;
}
#endif

Queue.h

*******************************

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;

// Queue template
template <class T>
class Queue
{
public:
T *queueArray; // Points to the queue array
int queueSize; // The queue size
int front; // Subscript of the queue front
int rear; // Subscript of the queue rear
int numItems; // Number of items in the queue
// Constructor
Queue(int);

// Copy constructor
Queue(const Queue &);

// Destructor
~Queue();

// Queue operations
void enqueue(T);
T dequeue();
bool isEmpty() const;
bool isFull() const;
void clear();
};

//***************************************************************
// This constructor creates an empty queue of a specified size. *
//***************************************************************
template <class T>
Queue<T>::Queue(int s)
{
queueArray = new T[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}

//***************************************************************
// Copy constructor *
//***************************************************************
template <class T>
Queue<T>::Queue(const Queue &obj)
{
// Allocate the queue array.
queueArray = new T[obj.queueSize];

// Copy the other object's attributes.
queueSize = obj.queueSize;
front = obj.front;
rear = obj.rear;
numItems = obj.numItems;

// Copy the other object's queue array.
for (int count = 0; count < obj.queueSize; count++)
queueArray[count] = obj.queueArray[count];
}

//***************************************************************
// Destructor *
//***************************************************************
template <class T>
Queue<T>::~Queue()
{
delete [] queueArray;
}

//***************************************************************
// Function enqueue inserts a value at the rear of the queue. *
//***************************************************************
template <class T>
void Queue<T>::enqueue(T item)
{
if (isFull())
cout << "The queue is full. ";
else
{
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = item;
// Update item count
numItems++;
}
}

//***************************************************************
// Function dequeue removes the value at the front of the queue *
// and copies t into num. *
//***************************************************************
template <class T>
T Queue<T>::dequeue()
{
if (isEmpty()) {
cout << "The queue is empty. ";
return '';
}
else
{
// Move front
front = (front + 1) % queueSize;
// Retrieve the front item
T item = queueArray[front];
// Update item count
numItems--;
return item;
}
}

//***************************************************************
// isEmpty returns true if the queue is empty, otherwise false. *
//***************************************************************
template <class T>
bool Queue<T>::isEmpty() const
{
bool status;

if (numItems)
status = false;
else
status = true;

return status;
}

//***************************************************************
// isFull returns true if the queue is full, otherwise false. *
//***************************************************************
template <class T>
bool Queue<T>::isFull() const
{
bool status;

if (numItems < queueSize)
status = false;
else
status = true;

return status;
}

//*****************************************************************
// clear sets the front and rear indices, and sets numItems to 0. *
//*****************************************************************
template <class T>
void Queue<T>::clear()
{
front = queueSize - 1;
rear = queueSize - 1;
numItems = 0;
}
#endif

Palindrome.h

*******************************

#ifndef _PALINDROME

#define _PALINDROME

// method protocol declaration

int isPalindrome(char *isPl);

#endif

Palindrome.cpp

*******************************

#include "Palindrome.h"

#include "Stack.h"

#include "Queue.h"

// method to check the input is palindrome or not

int isPalindrome(char *isPl)

{

// declare the required variables

int lp = 0;

Stack<char> palstk(100);

Queue<char> palqu(100);

// code to check the palindrome

while( isPl[lp] != '' )

{

if(isalpha(isPl[lp]))

{

palstk.push(toupper(isPl[lp]));

palqu.enqueue(toupper(isPl[lp]));

}

lp++;

}

while(!palstk.isEmpty())

{

if(palqu.dequeue()!=palstk.pop())

{


return 0;

}

}

return 1;

}

Main.cpp

*******************************

#include "Palindrome.h"

#include <cstdio>

// main method to invoke the functions to test palindrome

int main()

{

//Character array with a set of strings

char *palcheck[] = {

"Hello world", "A dog, a panic in a pagoda",

"A dog, a plan, a canal, pagoda",

"A man, a plan, a canal?? Panama!",

"civic",

"No lemons, no melon",

"racecar",

"RACEcar",

"Rats live on no evil star.",

"Red rum, sir, is murder!",

"Rise to vote, sir.",

"rotator",

};

// loop code to check each string is palindrome or not

for(int lp=0;lp<12;lp++)

{

printf("%s:",palcheck[lp]);

if(isPalindrome(palcheck[lp]))

printf("yes ");

else

printf("no ");

}

}

Explanation / Answer

======================

Stack.h

*******************************

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;

// Stack template
template <class T>
class Stack
{
public:
T *stackArray;
int stackSize;
int top;

//Constructor
Stack(int);

// Copy constructor
Stack(const Stack&);

// Destructor
~Stack();

// Stack operations
void push(T);
T pop();
bool isFull();
bool isEmpty();
};

//***************************************************
// Constructor *
//***************************************************

template <class T>
Stack<T>::Stack(int size)
{
stackArray = new T[size];
stackSize = size;
top = -1;
}

//***************************************************
// Copy constructor *
//***************************************************

template <class T>
Stack<T>::Stack(const Stack &obj)
{
// Create the stack array.
if (obj.stackSize > 0)
stackArray = new T[obj.stackSize];
else
stackArray = NULL;

// Copy the stackSize attribute.
stackSize = obj.stackSize;

// Copy the stack contents.
for (int count = 0; count < stackSize; count++)
stackArray[count] = obj.stackArray[count];

// Set the top of the stack.
top = obj.top;
}

//***************************************************
// Destructor *
//***************************************************

template <class T>
Stack<T>::~Stack()
{
if (stackSize > 0)
delete [] stackArray;
}

//*************************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************************

template <class T>
void Stack<T>::push(T item)
{
if (isFull())
{
cout << "The stack is full. ";
}
else
{
top++;
stackArray[top] = item;
}
}

//*************************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//*************************************************************

template <class T>
T Stack<T>::pop()
{
if (isEmpty())
{
cout << "The stack is empty. ";
return '';
}
else
{
T item = stackArray[top];
top--;
return item;
}
}

//*************************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//*************************************************************

template <class T>
bool Stack<T>::isFull()
{
bool status;

if (top == stackSize - 1)
status = true;
else
status = false;

return status;
}

//*************************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//*************************************************************

template <class T>
bool Stack<T>::isEmpty()
{
bool status;

if (top == -1)
status = true;
else
status = false;

return status;
}
#endif

Queue.h

*******************************

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;

// Queue template
template <class T>
class Queue
{
public:
T *queueArray; // Points to the queue array
int queueSize; // The queue size
int front; // Subscript of the queue front
int rear; // Subscript of the queue rear
int numItems; // Number of items in the queue
// Constructor
Queue(int);

// Copy constructor
Queue(const Queue &);

// Destructor
~Queue();

// Queue operations
void enqueue(T);
T dequeue();
bool isEmpty() const;
bool isFull() const;
void clear();
};

//***************************************************************
// This constructor creates an empty queue of a specified size. *
//***************************************************************
template <class T>
Queue<T>::Queue(int s)
{
queueArray = new T[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}

//***************************************************************
// Copy constructor *
//***************************************************************
template <class T>
Queue<T>::Queue(const Queue &obj)
{
// Allocate the queue array.
queueArray = new T[obj.queueSize];

// Copy the other object's attributes.
queueSize = obj.queueSize;
front = obj.front;
rear = obj.rear;
numItems = obj.numItems;

// Copy the other object's queue array.
for (int count = 0; count < obj.queueSize; count++)
queueArray[count] = obj.queueArray[count];
}

//***************************************************************
// Destructor *
//***************************************************************
template <class T>
Queue<T>::~Queue()
{
delete [] queueArray;
}

//***************************************************************
// Function enqueue inserts a value at the rear of the queue. *
//***************************************************************
template <class T>
void Queue<T>::enqueue(T item)
{
if (isFull())
cout << "The queue is full. ";
else
{
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = item;
// Update item count
numItems++;
}
}

//***************************************************************
// Function dequeue removes the value at the front of the queue *
// and copies t into num. *
//***************************************************************
template <class T>
T Queue<T>::dequeue()
{
if (isEmpty()) {
cout << "The queue is empty. ";
return '';
}
else
{
// Move front
front = (front + 1) % queueSize;
// Retrieve the front item
T item = queueArray[front];
// Update item count
numItems--;
return item;
}
}

//***************************************************************
// isEmpty returns true if the queue is empty, otherwise false. *
//***************************************************************
template <class T>
bool Queue<T>::isEmpty() const
{
bool status;

if (numItems)
status = false;
else
status = true;

return status;
}

//***************************************************************
// isFull returns true if the queue is full, otherwise false. *
//***************************************************************
template <class T>
bool Queue<T>::isFull() const
{
bool status;

if (numItems < queueSize)
status = false;
else
status = true;

return status;
}

//*****************************************************************
// clear sets the front and rear indices, and sets numItems to 0. *
//*****************************************************************
template <class T>
void Queue<T>::clear()
{
front = queueSize - 1;
rear = queueSize - 1;
numItems = 0;
}
#endif

Palindrome.h

*******************************

#ifndef _PALINDROME

#define _PALINDROME

// method protocol declaration

int isPalindrome(char *isPl);

#endif

Palindrome.cpp

*******************************

#include "Palindrome.h"

#include "Stack.h"

#include "Queue.h"

// method to check the input is palindrome or not

int isPalindrome(char *isPl)

{

// declare the required variables

int lp = 0;

Stack<char> palstk(100);

Queue<char> palqu(100);

// code to check the palindrome

while( isPl[lp] != '' )

{

if(isalpha(isPl[lp]))

{

palstk.push(toupper(isPl[lp]));

palqu.enqueue(toupper(isPl[lp]));

}

lp++;

}

while(!palstk.isEmpty())

{

if(palqu.dequeue()!=palstk.pop())

{


return 0;

}

}

return 1;

}

Main.cpp

*******************************

#include "Palindrome.h"

#include <cstdio>

// main method to invoke the functions to test palindrome

int main()

{

char str[100];
cout<<"Enter a String: ";
cin.getline(str,100);

printf("%s:",str);

if(isPalindrome(str))

printf("yes, Its a PALINDROME ");

else

printf("no, Its NOT A PALINDROME ");

}