Program C Repost Question Purpose To get familiar with imple
Program: C++
(Repost)
Question: Purpose: To get familiar with implementation of Qu...
Purpose: To get familiar with implementation of Queue class, and write a Queue application.
Assignment
We discussed Queue class in our lectures. In addition to what we discussed, please add the following functions to the Queue class that we discussed. Test it as you add each new operation. When you\'re done, use the program Qtester.cpp to test your class.
1.<overload the=\"\" output=\"\" operator=\"\" <<=\"\" so=\"\" that=\"\" a=\"\" statement=\"\" of=\"\" form=\"\" cout=\"\" q;=\"\" will=\"\" display=\"\" q\'s=\"\" contents=\"\" from=\"\" front=\"\" to=\"\" back.)
2. size() //returns the number of elements in the queue
3. back() // returns the element at the back of a queue
/*
tester.cpp
Program to test class Queue.
Input (keyboard): Commands entered by the user.
Output (screen): List of commands and messages describing
the effects of each command.
----------------------------------------------------------------*/
#include \"Queue.h\"
#include
#include
using namespace std;
/*--------------------------------------------------------------
Function to list the available commands.
Output (screen): List of commands.
----------------------------------------------------------------*/
void ShowCommands()
{
cout << \"Use the following commands to test the Queue class:\ \"
<< \"a --- add an element to the queue\ \"
<< \"d --- display contents of queue\ \"
<< \"e --- test whether a queue is empty\ \"
<< \"f --- retrieve the item at the front of the queue\ \"
<< \"h --- help -- print this list of commands\ \"
<< \"r --- remove item from front of the queue\ \"
<< \"s --- display the size of queue\ \"
<< \"b --- retrieve the item at the back of the queue\ \"
<< \"q --- quit testing\ \";
}
int main()
{
QueueElement item; // item to add to the queue
char command; // user command (selected from menu)
Queue q; // The QUEUE
ShowCommands();
do
{
cout << \"Command? \";
cin >> command;
if (isupper(command)) command = tolower(command);
switch(command)
{
case \'a\':
cout << \"Enter item to add to queue: \";
cin >> item;
q.enqueue(item);
cout << \"--> \" << item << \" added\ \";
break;
case \'d\':
cout << \"--> Queue contents:\ \" << q << endl;
break;
case \'e\':
cout << \"--> Queue \" << (q.empty() ? \"is\" : \"is not\")
<< \" empty\ \";
break;
case \'f\':
cout << \"--> \" << q.front() << \" is at the front\ \";
case \'h\':
ShowCommands();
break;
case \'r\':
q.dequeue();
cout << \"--> Front element removed\ \";
break;
case \'s\':
cout << \"--> the size of the queue is: \" << q.size() << endl;
break;
case \'b\':
cout << \"--> \" << q.back() << \" is at the back\ \";
break;
case \'q\':
cout << \"--> End of test\ \";
break;
default:
cout << \"*** Illegal command: \" << command << endl;
}
}
while (command != \'q\');
}
Check out your work to get credit for Closed Lab 7a.
Part b: Exercise Using stack and queue.
A string is said to be a palindrome if it does not change when the order of characters in the string is reversed. For example, MADAM, 45811854, and ABLE WAS I ERE I SAW ELBA are palindromes. Write a program that can read a number of sentences, each of which may contain spaces, numbers, punctuations. Use the Queue class and Stack class in your program to process each sentence, as the following procedures show: read a sentence, check each character to see if it is a letter or number, if it is, pushing the character onto a stack and add it to a queue at the same time. When the end of the sentence check is encountered, the program should use the basic stack and queue operations to determine if the string is a palindrome. Your program need to be able to read our provided text file, find which line is palindrome and which line is not, and output all of palindromes line by line to a new file.
(Can anyone help me with both parts as I still don\'t understand stack and queue and I\'ve been trying to do this for awhile now? Also to the person that help me, can you split section A and section B from one to another and also add comments so I can get a general idea as to what the code is doing?)
Solution
SOURCE CODE:
7a)
Queue.h
#include<iostream>
#define MAX_SIZE 1000
using namespace std;
class Queue{
public:
int front=-1,rear=-1;
char queue[MAX_SIZE];
void enqueue(char ele)
{
if(rear == MAX_SIZE-1)
{
cout<<\"\ Queue full\ \";
}
else
{
queue[++rear]=ele;
}
}
void dequeue()
{
if(front==rear)
{
cout<<\"\ Queue Empty\ \";
}
else
{
++front;
}
}
int size()
{
return rear-front+1;
}
int empty()
{
if(front==rear)
return 1;
return 0;
}
char back()
{
if(front==rear)
return \'-\';
return queue[rear];
}
char first()
{
if(front==rear)
return\'-\';
return queue[front+1];
}
friend ostream &operator<<(ostream &output, Queue &Q ) {
int i=Q.front;
if(i<=Q.rear)
cout<<\"\ Queue: \";
while(i<=Q.rear)
{
cout<<Q.queue[++i];
}
}
};
tester.cpp
#include \"Queue.h\"
using namespace std;
/*--------------------------------------------------------------
Function to list the available commands.
Output (screen): List of commands.
----------------------------------------------------------------*/
void ShowCommands()
{
cout << \"Use the following commands to test the Queue class:\ \"
<< \"a --- add an element to the queue\ \"
<< \"d --- display contents of queue\ \"
<< \"e --- test whether a queue is empty\ \"
<< \"f --- retrieve the item at the front of the queue\ \"
<< \"h --- help -- print this list of commands\ \"
<< \"r --- remove item from front of the queue\ \"
<< \"s --- display the size of queue\ \"
<< \"b --- retrieve the item at the back of the queue\ \"
<< \"q --- quit testing\ \";
}
int main()
{
char item; // item to add to the queue
char command; // user command (selected from menu)
Queue q; // The QUEUE
ShowCommands();
do
{
cout << \"Command? \";
cin >> command;
if (isupper(command)) command = tolower(command);
switch(command)
{
case \'a\':
cout << \"Enter item to add to queue: \";
cin >> item;
q.enqueue(item);
cout << \"--> \" << item << \" added\ \";
break;
case \'d\':
cout << \"--> Queue contents:\ \" << q << endl;
break;
case \'e\':
cout << \"--> Queue \" << (q.empty() ? \"is\" : \"is not\")
<< \" empty\ \";
break;
case \'f\':
cout << q.first() << \" is at the front\ \";
case \'h\':
ShowCommands();
break;
case \'r\':
q.dequeue();
cout << \"--> Front element removed\ \";
break;
case \'s\':
cout << \"--> the size of the queue is: \" << q.size() << endl;
break;
case \'b\':
cout << \"--> \" << q.back() << \" is at the back\ \";
break;
case \'q\':
cout << \"--> End of test\ \";
break;
default:
cout << \"*** Illegal command: \" << command << endl;
}
}while (command != \'q\');
return 1;
}
OUTPUT:
Use the following commands to test the Queue class:
a --- add an element to the queue
d --- display contents of queue
e --- test whether a queue is empty
f --- retrieve the item at the front of the queue
h --- help -- print this list of commands
r --- remove item from front of the queue
s --- display the size of queue
b --- retrieve the item at the back of the queue
q --- quit testing
Command? a
Enter item to add to queue: a
--> a added
Command? a
Enter item to add to queue: r
--> r added
Command? a
Enter item to add to queue: t
--> t added
Command? a
Enter item to add to queue: c
--> c added
Command? a
Enter item to add to queue: c
--> c added
Command? s
--> the size of the queue is: 5
Command? f
a is at the front
Use the following commands to test the Queue class:
a --- add an element to the queue
d --- display contents of queue
e --- test whether a queue is empty
f --- retrieve the item at the front of the queue
h --- help -- print this list of commands
r --- remove item from front of the queue
s --- display the size of queue
b --- retrieve the item at the back of the queue
q --- quit testing
Command? e
--> Queue is not empty
Command? r
--> Front element removed
Command? d
--> Queue contents:
Queue: rtcc
7b)
Stack.h
#include <iostream>
#define MAX_SIZE 1000
using namespace std;
class Stack{
public:
int top=-1;
char stack[1000];
void push(char ele)
{
if(top == MAX_SIZE-1)
{
}
else
{
stack[++top]=ele;
}
}
char pop()
{
if(top == -1)
{
return \'-\';
}
return stack[top--];
}
friend ostream &operator<<(ostream &output, Stack &S )
{
int i=S.top;
if(i>0)
{
cout<<\"\ Stack: \";
while(i>=0)
{
cout<<S.stack[i--];
}
}
}
int size()
{
return top+1;
}
int empty()
{
if(top==-1)
return 1;
return 0;
}
};
Palindrome.cpp
#include \"Queue.h\"
#include \"STACK.h\"
#include <iostream>
using namespace std;
int main()
{
int i=0;
string str=\"\";
Queue q;
Stack s;
cout<<\"Enter the string: \";
cin>>str;
for(i=0;i<str.length();i++)
{
q.enqueue(str.at(i));
s.push(str.at(i));
}
i=1;
while(1)
{
if(q.empty())
break;
if(q.first()!=s.pop())
{
i=0;
break;
}
q.dequeue();
}
if(i==0)
cout<<\"\ The given string is not a palindrome\";
else if(i==1)
cout<<\"\ The given string is a palindrome\";
return 1;
}
OUTPUT:
Enter the string: ABLE WAS I ERE I SAW ELBA
The given string is a palindrome








