Question

I have a queue and stack class program that deals with a palindrome, I need someone...

I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++.

Here is my program:

#include<iostream>
#include<list>
#include<iterator>
#include<string>
using namespace std;
class Queue
{
public:
list <char> queue;
Queue()
{
list <char> queue;
}
void Push(char item)
{
queue.push_back(item);
}
char pop()
{
char first = queue.front();
queue.pop_front();
return first;
}
bool is_empty()
{
if(queue.empty())
{
return true;
}
else
{
return false;
}
}
};
class Stack
{
public:
list <char> stack;
Stack()
{
list <char> stack;
}
void Push(char item)
{
stack.push_front(item);
}
char pop()
{
char first = stack.front();
stack.pop_front();
return first;
}
bool is_empty()
{
if(stack.empty())
{
return true;
}
else
{
return false;
}
}
};
bool is_palindrome(string alphabet)
{
Stack stack;
Queue queue;
int i = 0;
while(alphabet[i] != 0)
{
stack.Push(alphabet[i]);
queue.Push(alphabet[i]);
i++;
}
while(!stack.is_empty() && !queue.is_empty())
{
if(stack.pop() != queue.pop())
{
return false;
}
}
return true;
}
int main()
{
string alphabet;
cout << "Please enter the alphabet: \n";
getline (cin, alphabet);
if(is_palindrome(alphabet))
{
cout<<"The given string is a palindrome"<<endl;
}
else
{
cout<<"The given string is not a plaindrome"<<endl;
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to create template Stack and template Queue

#include<iostream>

#include<list>

#include<iterator>

#include<string>

using namespace std;

// create a generic Queue class

template <class T>

class Queue

{

private:

               list <T> queue;

public:

               Queue()

               {}

               void Push(T item)

               {

                              queue.push_back(item);

               }

               T pop()

               {

                              T first = queue.front();

                              queue.pop_front();

                              return first;

               }

               bool is_empty()

               {

                              return(queue.empty());

               }

};

// create a generic Stack class

template <class T>

class Stack

{

private:

               list <T> stack;

public:

               Stack()

               {}

               void Push(T item)

               {

                              stack.push_front(item);

               }

               T pop()

               {

                              T first = stack.front();

                              stack.pop_front();

                              return first;

               }

               bool is_empty()

               {

                              return(stack.empty());

               }

};

// function to determine the if the string is a palindrome or not

bool is_palindrome(string alphabet)

{

    // create Stack and Queue of characters

               Stack<char> stack;

               Queue<char> queue;

               int i = 0;

               while(alphabet[i] != 0)

               {

                              stack.Push(alphabet[i]);

                              queue.Push(alphabet[i]);

                              i++;

               }

               while(!stack.is_empty() && !queue.is_empty())

               {

                              if(stack.pop() != queue.pop())

                              {

                                             return false;

                              }

               }

               return true;

}

int main() {

               string alphabet;

               cout << "Please enter the string: \n";

               getline (cin, alphabet);

               if(is_palindrome(alphabet))

               {

                              cout<<"The given string is a palindrome"<<endl;

               }

               else

               {

                              cout<<"The given string is not a palindrome"<<endl;

               }

               return 0;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
I have a queue and stack class program that deals with a palindrome, I need someone...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can anyone help me fix this program to make sure use stack and use STL library...

    Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome. Thank you stackPalindrome.h #ifndef_STACK_PALINDROME_H #define_STACK_PALINDROME_H template <class StackPalindrome> class StackPalindrome { public:    virtual bool isEmpty() const = 0;    virtual bool push(const ItemType& newEntry) = 0;    virtual bool pop() = 0;    virtual ItemType peek() const = 0; }; #endif stack.cpp #include<iostream> #include<string> #include<new> #include "stackPalindrome.h" using namespace std; template <class ItemType> class OurStack...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Fix the function so that it checks if the string is a palindrome #include <iostream> using...

    Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false;    if (/* add the condition */) return true;    //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x;    if (is_palindrome(x,x.length())){...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT