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.
Im stuck bc I need to say when it is a Palindrome and when I run it it's out of range.
Code:
#include<iostream>
#include<conio.h>
#include<string>
#include<ctype.h>
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()
{
Astack S;
AQueue Q;
string s;
int i = 0;
cout << "Enter String" << endl;
getline(cin, s);
for(i = 0; i < s.length(); i++){
cout << s.at(i);
while(s.at(i) != NULL){
S.push(s.at(i));
Q.enqueue(s.at(i));
i++; /*Get next character*/
}
while(s.at(i) == NULL){
if(S.topValue(s.at(i)) != Q.frontValue(s.at(i)))
cout << "It is not a Palindrome";
else
S.pop(s.at(i));
Q.dequeue(s.at(i));
}
}
return 0;
}