Question Need help with program. Need it C++. Write a program that reads a line
ID: 3682109 • Letter: Q
Question
Question
Need help with program. Need it C++.
Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in queue and onto a stack.
The string should consist of printable characters.
Use the linked-based stack and linked-based queue class from the textbook.
Character strings to test are in the data file lab6.txt.
For example, "radar", "toot", and "mjk$x$kjm" are character string palindromes, while "panama" and "tool" are not.
The program should:
1. Print test values (lab6.txt) and verify whether the test string values are palindrome
2. Print user input value and verify whether the line of text is palindrome
//////////////////////////////////////////////////////////////////////////
linkedQueue.h
///////////////////////////////////////////////////
linkedStackType.h
/////////////////////////////////////////
lab6.txt
Explanation / Answer
//Header file linkedQueue.h
#ifndef NodeType
#define NodeType
//Definition of the node
template <class Type>
struct nodeType // struct for storing some item
{ // in a linked list
Type info;
nodeType<Type> *link;
};
#endif
#ifndef H_linkedQueue
#define H_linkedQueue
#include <cassert>
#include <iostream>
#include <new>
#include <cstddef>
using namespace std;
template <class Type>
class linkedQueueType
{
public:
const linkedQueueType<Type>& operator=(const linkedQueueType<Type>&);
int isEmptyQueue() const {return ((queueFront) ? 0 : 1);};
int isFullQueue() const;
void initializeQueue();
Type front() const;
Type back() const;
linkedQueueType<Type>& addQueue(const Type & newElement);
void deleteQueue();
linkedQueueType() {queueFront = queueRear = NULL;};
linkedQueueType(const linkedQueueType<Type>& otherQueue); // copy constructor
~linkedQueueType(); //destructor
private:
nodeType<Type> *lastElem;
nodeType<Type> *queueFront;
nodeType<Type> *queueRear;
int count;
nodeType<Type> * CopyList(const nodeType<Type> * ptr) const;
};
// Default constructor
template<class Type>
linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue)
{
if(otherQueue.queueFront == NULL)
{
queueFront = NULL;
queueRear = NULL;
}
else
{
queueFront = CopyList(otherQueue.queueFront);
queueRear = queueFront;
}
while (queueRear->next != NULL)
{
queueRear = queueRear->next;
}
}
template<class Type>
bool linkedQueueType<Type>::isEmptyQueue() const
{
return(queueFront == NULL);
}
template<class Type>
int linkedQueueType<Type>::isFullQueue()
{
return count;
}
template<class Type>
void linkedQueueType<Type>::initializeQueue()
{
nodeType<Type> *temp;
while (queueFront!= NULL) //while there are elements left
//in the queue
{
temp = queueFront; //set temp to point to the
//current node
queueFront = queueFront->link; //advance first to
//the next node
delete temp; //deallocate memory occupied by temp
}
queueRear = NULL; //set rear to NULL
}
template<class Type>
linkedQueueType<Type>& linkedQueueType<Type>::addQueue(const Type & newElement)
{
nodeType<Type> *ptr;
ptr = new nodeType<Type>;
ptr->item = newElement;
ptr->next = NULL;
if (queueFront)
{
queueRear->next = ptr;
}
else
{
queueFront = ptr;
queueRear = ptr;
}
count++;
return *this;
}
template<class Type>
Type linkedQueueType<Type>::front() const
{
if (isEmptyQueue())
{
cout<<"out of bounds";
return -1;
}
return queueFront->item;
}
template<class Type>
Type linkedQueueType<Type>::back() const
{
if (isEmptyQueue())
{
cout<<"out of bounds";
return -1;
}
return queueRear->item;
}
template<class Type>
void linkedQueueType<Type>::deleteQueue()
{
nodeType<Type> *temp;
if (!isEmptyQueue())
{
temp = queueFront; //make temp point to the
//first node
queueFront = queueFront->link; //advance queueFront
delete temp; //delete the first node
if (queueFront == NULL) //if after deletion the
//queue is empty
queueRear = NULL; //set queueRear to NULL
}
else
cout << "Cannot remove from an empty queue" << endl;
}
template <class Type>
linkedQueueType<Type>::~linkedQueueType()
{
// Queue destructor. Delete all nodes.
nodeType<Type> *link;
while (queueFront) {
link = queueFront->next;
delete queueFront;
queueFront = link;
}
}
template <class Type>
void linkedQueueType<Type>::copyQueue
(const linkedQueueType<Type>& otherQueue) {
nodeType<Type> *newNode, *current, *last;
if (queueFront != NULL) //if stack is nonempty, make it empty
initializeQueue();
if (otherQueue.queueFront == NULL)
queueFront = NULL;
else {
current = otherQueue.queueFront; //set current to point
//to the stack to be copied
//copy the stackTop element of the stack
queueFront = new nodeType<Type>; //create the node
queueFront->info = current->info; //copy the info
queueFront->link = NULL; //set the link field of the
//node to NULL
last = queueFront; //set last to point to the node
current = current->link; //set current to point to
//the next node
//copy the remaining stack
while (current != NULL) {
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}//end while
queueRear = last;
}//end else
}
template <class Type>
void linkedQueueType<Type>::printQueue() const {
nodeType<Type> *temp;
temp = queueFront;
while(temp != NULL) {
cout << temp->info;
temp = temp->link;
}
}
template<class Type>
const linkedQueueType<Type>& linkedQueueType<Type>::operator=(const linkedQueueType<Type>& otherQueue)
{
if (this != &otherQueue) //avoid self-copy
{
if (otherQueue.queueFront == NULL)
{
queueFront = NULL;
queueRear = NULL;
}
else
{
queueFront = CopyList(otherQueue.queueFront);
queueRear = queueFront;
while (queueRear->next != NULL)
{
queueRear = queueRear->next;
}
}
}
return *this;
}
template <class Type>
nodeType<Type> * linkedQueueType<Type>::CopyList(const nodeType<Type> * ptr) const
{
if (ptr == NULL)
{
return NULL;
}
else {
nodeType<Type> * temp = new nodeType<Type>;
temp->item = ptr->item;
temp->next = CopyList(ptr->next);
return temp;
}
}
#endif
#include <iostream>
#include "linkedQueue.h"
using namespace std;
int size = 1000;
int main()
{
linkedQueueType<int> lLQue, lLQue1;
int x, y;
lLQue.initializeQueue();
x = 4;
y = 5;
lLQue.addQueue(x);
lLQue.addQueue(y);
x = lLQue.front();
lLQue.deleteQueue();
lLQue.addQueue(x + 5);
lLQue.addQueue(16);
lLQue.addQueue(x);
lLQue.addQueue(y - 3);
lLQue.addQueue(23);
cout << lLQue.queueCount() << endl;
cout << lLQue1.queueCount() << endl;
lLQue1 = lLQue;
cout << lLQue.queueCount() << endl;
cout << lLQue1.queueCount() << endl;
while (!lLQue.isEmptyQueue())
{
cout << lLQue.front() << " ";
lLQue.deleteQueue();
}
cout << endl;
cout << "lLQue1: ";
while (!lLQue1.isEmptyQueue())
{
cout << lLQue1.front() << " ";
lLQue1.deleteQueue();
}
cout << endl;
return 0;
}
and you have provided same prototype as linkedQueueType.h instead of linkedStackType.h it might have following methods
public:
void initializeStack();
bool isEmptyStack() const;
bool isFullStack() const;
void push(const Type& newItem);
Type top() const;
void pop();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.