In: Computer Science
C++
Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). Please use a Queue Class and Stack class.
------------------MyQueue.h----------------
#include<iostream>
using namespace std;
class MyQueue
{
char *queue;
int front;
int rear;
int max;
public:
MyQueue(int max)
{
this->max = max;
// create an int array os size max
this->queue = new char[ max ];
this->front = -1;
this->rear = -1;
}
// destructor
~MyQueue()
{
delete this->queue;
}
void enqueue(char i)
{
// if the queue is full
if( rear == max - 1 )
return;
else
{
// add element to queue
this->queue[++this->rear] = i;
}
}
void dequeue()
{
// if queue is empty
if( this->front != this->rear )
this->front++;
}
char top()
{
// if queue is empty
if( this->front == this->rear )
return -1;
else
return this->queue[ this->front + 1 ];
}
// check if queue is empty
bool isEmpty()
{
return this->front == this->rear;
}
};
---------------------Stack.h---------------------
#ifndef MY_STACK
#define MY_STACK
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class Stack
{
// declare stack array arr
char *arr;
// to store the index of the top most element
int top;
// to store the current capacity of arr
int size;
public:
// constructor
Stack();
// constructor
Stack(int);
// destructor
~Stack();
// function to check if the stack is empty
bool isEmpty();
// function to check if the stack is full
bool isFull();
// push element intot the stack
void push(char);
// return the top most element in the stack
char peek();
// pop element from stack
void pop();
// resize the array to double the original capacity
void resize();
// display contents of stack
void view();
};
#endif
-------------------Stack.cpp--------------------
#include "Stack.h"
// constructor
Stack::Stack()
{
// declare an array of size 4
arr = new char[4];
// set initial capacity to 4
size = 4;
top = -1;
}
// constructor
Stack::Stack(int new_size)
{
// declare an array of size new_size
arr = new char[new_size];
// set initial capacity to new_size
this->size = new_size;
top = -1;
}
// destructor
Stack::~Stack()
{
delete arr;
size = 0;
top = -1;
}
// function to check if the stack is empty
bool Stack::isEmpty()
{
if(top == -1)
return true;
return false;
}
// function to check if the stack is full
bool Stack::isFull()
{
if(top == size - 1)
return true;
return false;
}
void Stack::push(char n)
{
// if stack is full
if(isFull())
// resize the array to double the original capacity
resize();
arr[++top] = n;
}
// return the top most element in the stack
char Stack::peek()
{
if(isEmpty())
return -1;
return arr[top];
}
// pop element from stack
void Stack::pop()
{
// if stack is empty
if(isEmpty())
cout<<"The stack is alreasy empty";
// remove the top most element of the stack
top--;
}
// resize the array to double the original capacity
void Stack::resize()
{
// create a new int array double the size of arr
char *temp = new char[2 * size];
int i;
for( i = 0 ; i < size ; i++ )
// cop contents of arr to temp
temp[i] = arr[i];
// set capacity to double the original capacity
size *= 2;
// set temp as the new stack array
arr = temp;
}
// display contents of stack
void Stack::view()
{
int i;
for( i = 0 ; i <= top ; i++ )
cout<<arr[i]<<" ";
cout<<endl;
}
-------------------main.cpp----------------
#include "Stack.cpp"
#include "MyQueue.h"
#include<bits/stdc++.h>
bool isPalindrome()
{
// remains true is string is palindrome
bool flag = true;
// create queue of size 10000
MyQueue q(10000);
Stack st;
string temp;
cout<<"Enter string ; ";
// read complete line
getline( cin , temp );
// convert string to lower case
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
int i, len = 0;
for( i = 0 ; i < temp.length() ; i++ )
{
// if the current character is not space
if( temp[i] != ' ' )
{
// convert the current character to lower case and addd to queue
q.enqueue((char)tolower(temp[i]));
// convert the current character to lower case and addd to stack
st.push((char)tolower(temp[i]));
}
}
// loop untill the queue is empty
while( !q.isEmpty() )
{
char ch1 = q.top();
char ch2 = st.peek();
q.dequeue();
st.pop();
if( ch1 != ch2 )
{
flag = false;
break;
}
}
return flag;
}
int main()
{
if( isPalindrome() )
cout<<"It is palindrome.";
else
cout<<"it is not palindrome.";
return 0;
}
Sample Output