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

As you may recall, the implementation of the cs20::Stack< Object > data structur

ID: 3846890 • Letter: A

Question

As you may recall, the implementation of the cs20::Stack< Object > data structure presented in class used a linked node structure following an identified top node. Based on this data structure, write the following method:

template <class Object>
bool Stack<Object>::isPalindrome( ) const;

This method should determine whether the Stack reads the same from topNode to the end as it does from the end backwards to the topNode. Feel free to define any public or private methods or members on the class Stack<Object> as you need. Please work with the code from class.

For example, please consider the stack shown below:

However, for the stack shown below:

files are provided in the link below

https://dropfile.to/8SHzxee acces key: fj1OyFE

https://dropfile.to/v4Dv9uh acces key:   6u6LeTP

TopNode 12 15 22 37

Explanation / Answer

#include <string>
#include <iostream>
#define STACKSIZE 80
#define TRUE 1
#define FALSE 0
using namespace std;
//Creates a class Stack
class Stack
{
public:
//Default Constructor
Stack();
//Check stack is empty or not
int IsStackEmpty();
//Checks stack is full or not
int IsStackFull();
//Adds a number to the stack
void StackPush(int c);
//Extracts a number from stack
int StackPop();
//Checks the number entered is palindrome or not
bool isPalindrome();
//User defined size
int size;
//To store numbers entered by the user
int palin[STACKSIZE];
private:
//For store numbers in stack
int myStack[STACKSIZE];
//For top position of stack
int top;
};//End of class

//Default constructor to initialize top to -1
Stack::Stack()
{
top = -1;
}//End of constructor

//Returns 1 if the stack is empty
int Stack::IsStackEmpty()
{
//Checks if the top is -1 return true
if (top == -1)
return TRUE;
//Otherwise return false
return FALSE;
}//End of function

//Returns 1 if the stack is full
int Stack::IsStackFull()
{
//Checks if the top is equal to stack size -1 return true
if (top == (size - 1))
return TRUE;
//Otherwise returns false
return FALSE;
}//End of function

//Adds a number to the stack top
void Stack::StackPush(int c)
{
//Checks if the stack is full or not
if (!IsStackFull())
//Add a number given as parameter to the stack top and increase the top by one
myStack[++top] = c;
}//End of function

//Returns the stack top position value
int Stack::StackPop()
{
//Checks whether the stack is empty or not
if(!IsStackEmpty())
//Returns the stack top position value and decrease the top by one
return myStack[top--];
}//End of function

//Returns true if palindrome otherwise return false
bool Stack::isPalindrome()
{
//Initialize the flag to zero
int flag = 0;
//To store stack top value
int num;
//Loops till stack size
for(int x = 0; x < size; x++)
{
//Extracts the stack top value and stores it in num
num = StackPop();
//Checks stack top value is not equal to original entered array's x position
if(num != palin[x])
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of loop
//Checks if the flag is zero
if(flag == 0)
//Returns true
return true;
//Otherwise returns false
else
return false;
}//End of function

//Main function
int main()
{
//To store number entered by the user
int num;
//Stack class object created
Stack st;
//Accepts how many number user wants
cout <<" Enter how many numbers you want to insert: ";
cin>>st.size;
//Accepts numbers
cout<<" Enter "<<st.size<<" numbers: ";
//Loops till user entered size
for(int x = 0; x < st.size; x++)
{
//Accept a number
cin>>num;
//Calls push method to add to the stack
st.StackPush(num);
//Stores the number in array
st.palin[x] = num;
}//End of for loop
//Calls the palindrome function to check palindrome
if(st.isPalindrome())
cout<<" Palindrome";
else
cout<<" Not Palindrome";
return 0;
}//End of main

Sample Run 1:

Enter how many numbers you want to insert: 4

Enter 4 numbers: 12
15
22
37

Not Palindrome

Sample Run 2:

Enter how many numbers you want to insert: 4

Enter 4 numbers: 12
1
1
12

Palindrome

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote