Write in C++ Instructions In this assignment, you will implement a node-based st
ID: 3747960 • Letter: W
Question
Write in C++
Instructions In this assignment, you will implement a node-based stack and use the implementation to solve the palindrome problem. A palindrome is a special word or phrase that when reversed produces the original word. In other words, if we have a string w, w is a palindrome if Wr reverse(w) AND w wr For example, "mom', is palindrome because reverse("mom")-“mom", and “mom"-“mom." Other palindromes include: 1. Step on no pets 2. Kayak 3. Noon 4. No word, no bond, row on. (Note: only a palindrome if spaces are removed). For this assignment, your Stack class does not have to be templated. Data in each node of the stack should be a "char." The following is a UML diagram that describes the methods and attributes needed for each class:Explanation / Answer
//C++ program
#include<iostream>
using namespace std;
class Node{
char data;
Node* below;
public:
Node(){
below=NULL;
}
Node(char d , Node * b){
data = d;
below = b;
}
Node* getBelow(){
return below;
}
char getData(){
return data;
}
};
class Stack{
Node * top;
int size;
public:
Stack(){
top=NULL;
size=0;
}
Stack(char data){
top = new Node(data , NULL);
size++;
}
void push(char data){
top = new Node(data , top);
size++;
}
char pop(){
Node * temp=top;
top=top->getBelow();
char val=temp->getData();
delete temp;
size--;
return val;
}
string ToString (){
string str="";
while(top){
str+=pop();
}
return str;
}
};
int main(){
Stack st;
string str1="step on no pets",str2;
for(int i=0;i<str1.length();i++){
st.push(str1[i]);
}
str2=st.ToString();
if(str1==str2)cout<<str1<<" is a palindrom ";
else cout<<str1 <<" is not a palindrom ";
str1="no word,no bond ,row on";
for(int i=0;i<str1.length();i++){
st.push(str1[i]);
}
str2=st.ToString();
if(str1==str2)cout<<str1<<" is a palindrom ";
else cout<<str1 <<" is not a palindrom ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.