Hi, I am new to stacks and need some help getting started with my first program in stacks. I have all the functions like pop,push etc. in my header file. I have to create a palindrome program with the objectives below. If someone can give me an idea on how to start this program, I appreciate it. I added a main.cpp of a palindrome program that I did using recursion if that will give me a push on what I need to do. Also, do you declare a stack like this?
stack u;
stack r;
stack s;
Objectives:
1. As a line is read, push each character on a stack(s) and stack(u).
2. Transfer the info from stack(u) to stack(r) so that stack(r) is the reverse of stack(u)
3. Repeatedly pop elements from stack(s) and stack(r) to test equality
4. If equal, it prints "is a palindrome", if almost equal, it prints "is almost a palindrome, if not a palindrome, it prints "is not a palindrome"
Expected Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z is not a palindrome
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 is not a palindrome
a b b a c c a c c a b b a is a palindrome
1 2 3 3 2 1 2 3 3 2 1 is a palindrome
1 2 3 2 1 2 4 2 1 is almost a palindrome
Input File:
a b c d e f g h i j k l m n o p q r s t u v w x y z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
a b b a c c a c c a b b a
1 2 3 3 2 1 2 3 3 2 1
1 2 3 2 1 2 4 2 1
main.cpp
#include <fstream>
#include <string>
#include "stackarray.h"
using namespace std;
bool Palindrome (string pali, int paliIndex, int paliLength, ofstream& outData);
int main()
{
string words;
ifstream inData("input.txt");
ofstream outData("output.txt");
while ( getline( inData, words ) )
{
Palindrome( words, 0, words.length(),outData );
}
inData.close();
outData.close();
system("PAUSE");
return 0;
}
bool Palindrome (string pali, int paliIndex, int paliLength, ofstream& outData)
{
if((paliLength == 0 || paliLength == 1))
{
outData << pali << " is a palindrome." << endl;
return true;
}
if( pali[paliIndex] == pali[paliLength - 1] )
{
return Palindrome(pali,paliIndex+1,paliLength-1…
}
else
{
outData << pali << " is not a palindrome." << endl;
return false;
}
}
.h file
#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>
using namespace std;
template<class Type>
class stackType
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void destroyStack();
void push(const Type& newItem);
Type top();
void pop();
stackType(int stackSize = 100);
stackType(const stackType<Type>& otherStack);
~stackType();
private:
int maxStackSize;
int stackTop;
Type *list;
void copyStack(const stackType<Type>& otherStack);
};
template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
assert(list != NULL);
for(int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this;
}
template<class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}
template<class Type>
void stackType<Type>::destroyStack()
{
stackTop = 0;
}
template<class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == maxStackSize);
}
template<class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize);
}
template<class Type>
void stackType<Type>::push(const Type& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cerr<<"Cannot add to a full stack."<<endl;
}
template<class Type>
Type stackType<Type>::top()
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template<class Type>
void stackType<Type>::pop()
{
if(!isEmptyStack())
{
stackTop--;
}
else
cerr<<"Cannot remove from an empty stack."<<endl;
}
template<class Type>
stackType<Type>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cerr<<"The size of the array to hold the stack must "
<<"be positive."<<endl;
cerr<<"Creating an array of size 100."<<endl;
maxStackSize = 100;