Write a program that uses both a Stack and a Queue to determine if a number of lines in Text File are packed palindromes or not. A packed palindrome is a sequence of characters with the property: If we remove all the characters except the letters (you may assume all capitals), then the resulting string when reversed is the same as it was before reversal.
Restrictions: You must use the Circular Array Implementation of the Queue. You may choose which implementation you want for the Stack.
Test program on the following:
YOUR NAME
A
ABA
ABC
ABBA
X?Y!X
MADAM. I'M ADAM
A MAN, A PLAN, A CANAL. PANAMA!
STRAW? NO, TOO STUPID! I PUT SOOT ON WARTS.
RACE CAR
Here's my code:
Code:
#include <iostream>
#include<conio.h>
#include <string>
using namespace std;
const int DefaultListSize = 50;
typedef char Elem;
class Astack
{
private:
int top; /*Index for top element*/
int size; /*Maximum size of stack*/
Elem*listArray; /*Array holding stack elements*/
public:
Astack(int sz =DefaultListSize) /*Constructor*/
{size = sz; top = 0; listArray = new Elem[sz];}
~Astack() { delete [] listArray;} /*Destructor*/
void clear() {top = 0;}
bool push(const Elem& item){
if(top == size) return false; /* Stack is full*/
else {listArray [top++] = item;
return true;
}
}
bool pop(Elem& item){ /*Pop top element*/
if(top == 0) return false;
else {item = listArray[--top];
return true;
}
}
bool topValue(Elem& item) const { /*Return top element*/
if (top == 0) return false;
else {item = listArray[top - 1];
return true;
}
}
int length() const {return top;}
bool IsEmpty() const {if(top == 0) return true;
else return false;
}
};
class AQueue
{
private:
int size; /*Maximum size of queue*/
int front; /*Index of front element*/
int rear; /*Index of rear element*/
Elem*listArray; /*Array holding queue elements*/
public:
AQueue(int sz =DefaultListSize){ /*Constructor*/
size = sz + 1;
rear = 0;
front = 1;
listArray = new Elem[size];
}
~AQueue() {delete [] listArray; } /*Destructor*/
void clear() {front = rear; }
bool enqueue(const Elem& it) {
if(((rear+2) % size) == front) return false; /*Full*/
rear = (rear+1) % size; /*Circular increment*/
listArray[rear] = it;
return true;
}
bool dequeue(Elem& it) {
if (length() == 0) return false; /*Empty*/
it = listArray[front];
front = (front+1) % size; /*Circular increment*/
return true;
}
bool frontValue(Elem& it) const {
if (length() == 0) return false; /*Empty*/
it = listArray[front];
return true;
}
virtual int length() const
{ return ((rear+size) - front+1) % size; }
};
int main()
{
char str[100];
bool flag;
Astack S;
AQueue Q;
cout<<"Enter word :";
cin>>str;
int x=strlen(str)-1;
for(int i=0;i<=x;i++)
{
if(str[i]==str[x-i])
{
flag=true;
continue;
}
else
{
flag=false;
break;
}
}
if(flag==true)
cout<<"Palindrome"<<endl;
else
cout<<"Not a palindrome"<<endl;
getch();
}
I need to figure where to push and pop my string through the Stack and Queue. And I need to figure out how to ignore the anything that is not a letter.