In: Computer Science
Please complete it in C++
Part 2: Queue
The following shows a number of program's sample input / output sessions.
Input a string: aibohphobia
aibohphobia is a palindrome
Input a string: level
level is a palindrome
Input a string: desmond
desmond is not a palindrome
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.h
#ifndef STACKARR_H
#define STACKARR_H
class StackArr {
private:
int maxTop;
int stackTop;
char *values;
public:
StackArr(int);
~StackArr();
bool isEmpty() const;
bool isFull() const;
char top() const;
void push(const char& x);
char pop();
void displayStack() const;
};
#endif
#pragma once
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.cpp
#include "StackArr.h"
#include <string>
#include <iostream>
using namespace std;
StackArr::StackArr(int size) {
maxTop = size;
values = new char[size];
stackTop = -1;
}
StackArr::~StackArr() {
delete[] values;
}
bool StackArr::isEmpty() const {
return stackTop == -1;
}
bool StackArr::isFull() const {
return stackTop == maxTop;
}
void StackArr::push(const char& x) {
if (isFull())
cout << "Error! The stack is
full!" << endl;
else
values[++stackTop] = x;
}
char StackArr::pop() {
if (isEmpty()) {
cout << "Error! The stack is
empty!" << endl;
return -1;
}
else
return values[stackTop--];
}
char StackArr::top() const {
if (isEmpty()) {
cout << "Error! The stack is
empty!" << endl;
return -1;
}
else
return values[stackTop];
}
void StackArr::displayStack() const {
cout << "Top -->";
for (int i = stackTop; i >= 0; i--)
cout << "\t|\t" <<
values[i] << "\t|" << endl;
cout << "\t|---------------|" << endl
<< endl;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.h
#ifndef QUEUEARR_H
#define QUEUEARR_H
class QueueArr {
private:
int front;
int back;
int counter;
int maxSize;
char* values;
public:
QueueArr(int);
~QueueArr();
bool isEmpty() const;
bool isFull() const;
bool enqueue(char x);
char dequeue();
void displayQueue() const;
};
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.cpp
#include "QueueArr.h"
#include <string>
#include <iostream>
using namespace std;
QueueArr::QueueArr(int size) {
values = new char[size];
maxSize = size;
front = 0;
back = -1;
counter = 0;
}
QueueArr::~QueueArr() {
delete[] values;
}
bool QueueArr::isEmpty() const {
if (counter)
return false;
else
return true;
}
bool QueueArr::isFull() const {
if (counter < maxSize)
return false;
else
return true;
}
bool QueueArr::enqueue(char x) {
if (isFull()) {
cout << "Error! The queue is
full." << endl;
return false;
}
else {
back = (back + 1) % maxSize;
values[back] = x;
counter++;
return true;
}
}
char QueueArr::dequeue() {
char x = -1;
if (isEmpty()) {
cout << "Error! The queue is
empty." << endl;
return -1;
}
else {
x = values[front];
front = (front + 1) %
maxSize;
counter--;
return x;
}
}
void QueueArr::displayQueue() const {
cout << "Front -->";
for (int i = 0; i < counter; i++) {
if (i == 0)
cout <<
"\t";
else
cout <<
"\t\t";
cout << values[(front + i) %
maxSize];
if (i != counter - 1)
cout <<
endl;
else
cout <<
"\t<--Back" << endl;
}
cout << endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.h
#ifndef STACKARR_H
#define STACKARR_H
class StackArr {
private:
int maxTop;
int stackTop;
char *values;
public:
StackArr(int);
~StackArr();
bool isEmpty() const;
bool isFull() const;
char top() const;
void push(const char& x);
char pop();
void displayStack() const;
};
#endif
#pragma once
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.cpp
#include "StackArr.h"
#include <string>
#include <iostream>
using namespace std;
StackArr::StackArr(int size) {
maxTop = size;
values = new char[size];
stackTop = -1;
}
StackArr::~StackArr() {
delete[] values;
}
bool StackArr::isEmpty() const {
return stackTop == -1;
}
bool StackArr::isFull() const {
return stackTop == maxTop;
}
void StackArr::push(const char& x) {
if (isFull())
cout << "Error! The stack is
full!" << endl;
else
values[++stackTop] = x;
}
char StackArr::pop() {
if (isEmpty()) {
cout << "Error! The stack is
empty!" << endl;
return -1;
}
else
return values[stackTop--];
}
char StackArr::top() const {
if (isEmpty()) {
cout << "Error! The stack is
empty!" << endl;
return -1;
}
else
return values[stackTop];
}
void StackArr::displayStack() const {
cout << "Top -->";
for (int i = stackTop; i >= 0; i--)
cout << "\t|\t" <<
values[i] << "\t|" << endl;
cout << "\t|---------------|" << endl
<< endl;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.h
#ifndef QUEUEARR_H
#define QUEUEARR_H
class QueueArr {
private:
int front;
int back;
int counter;
int maxSize;
char* values;
public:
QueueArr(int);
~QueueArr();
bool isEmpty() const;
bool isFull() const;
bool enqueue(char x);
char dequeue();
void displayQueue() const;
};
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.cpp
#include "QueueArr.h"
#include <string>
#include <iostream>
using namespace std;
QueueArr::QueueArr(int size) {
values = new char[size];
maxSize = size;
front = 0;
back = -1;
counter = 0;
}
QueueArr::~QueueArr() {
delete[] values;
}
bool QueueArr::isEmpty() const {
if (counter)
return false;
else
return true;
}
bool QueueArr::isFull() const {
if (counter < maxSize)
return false;
else
return true;
}
bool QueueArr::enqueue(char x) {
if (isFull()) {
cout << "Error! The queue is
full." << endl;
return false;
}
else {
back = (back + 1) % maxSize;
values[back] = x;
counter++;
return true;
}
}
char QueueArr::dequeue() {
char x = -1;
if (isEmpty()) {
cout << "Error! The queue is
empty." << endl;
return -1;
}
else {
x = values[front];
front = (front + 1) %
maxSize;
counter--;
return x;
}
}
void QueueArr::displayQueue() const {
cout << "Front -->";
for (int i = 0; i < counter; i++) {
if (i == 0)
cout <<
"\t";
else
cout <<
"\t\t";
cout << values[(front + i) %
maxSize];
if (i != counter - 1)
cout <<
endl;
else
cout <<
"\t<--Back" << endl;
}
cout << endl;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueMain.cpp
#include <cstring>
#include <iostream>
#include "QueueArr.cpp"
#include "StackArr.cpp"
using namespace std;
int main()
{
string str;/*a string to take input from user*/
cout<<"Enter the string : ";
cin>>str;
int n=str.size();/*storing the size of string*/
StackArr s(n);/*declare a stack object*/
QueueArr q(n);/*declare a queue object*/
for(int i=0;i<n;i++)/*converting each letter to
lowercase*/
{
q.enqueue(tolower(str[i]);/*enqueue the input string to queue in
order*/
s.push(tolower(str[i]);/*push the string in order on stack*/
}
/*display the content of stack and queue*/
q.displayQueue();
s.displayStack();
bool flg=1;/*flg to check if the input string is palindrome*/
while((n--)/2)/*half the size of the string needs to be checked if
it's a palindrome*/
{
if(q.dequeue()!=s.pop())/*using both queue and stack to determine
if it's a palindrome, stack will start popping the string from end
and queue will dequeue from beginning and both will move to the
middle of input*/
{
flg=0;
break;/*break if any unmatched character occurs*/
}
}
if(flg)/*string was palindrome if all characters from beginning to
mid(first half) and from end to mid(second half) matched and flg
remained 1 else it will be set to 0*/
{
cout<<str<<" is a palindrome";
}
else
{
cout<<str<<" is not a palindrome";
}
return 0;
}
//output