Question
This assessment will use queue to compress a string by removing spaces in it. A string str that contains string The general furniture was profuse,
This assessment will use queue to compress a string by removing spaces in it. A string str that contains string "The general furniture was profuse, comfortless, antique, and tattered." and a queue, q are given in the main(). The following steps show how to remove spaces in a string using a queue. 1. Go through every character in string str and check if it is a space. If the character is not a space, then enqueue it to the queue. Otherwise, dont enqueue to the queue. You need to keep track of the number of spaces encountered so it can be displayed in the output as shown in Figure 1. 2. Dequeue each character in the queue until queue is empty. Each character dequeue from the queue has to be copy to str starting from the first character. 3. Append \0 after the last character in str to indicate end of string. 4. Print the number of spaces removed from the queue and the content in str as shown.
Queue.cpp #include
Queue::Queue() { head = tail = NULL; count = 0; }
bool Queue::empty() { if (count == 0) return true; return false; }
int Queue::size() { return count; }
bool Queue::enqueue(type newItem) {// Any simplification can be done on code below? Node *tmp = new Node(newItem);
if (!tmp) return false; if (empty()) { head = tail = tmp; count++; return true; } count++; tail->next = tmp; tail = tmp; return true; }
bool Queue::dequeue(type &itemExtracted) { Node *cur = head;
if (empty()) return false; count--; itemExtracted = head->item; head = head->next; free(cur); if (count == 0) tail = NULL;
return true; }
Node *Queue::find(type record) { Node *cur;
cur = head; for (; cur != NULL;) { if (cur->item == record) return cur; cur = cur->next; } return NULL; }
Queue.h #ifndef Queue_type #define Queue_type
#include "Node.h"
struct Queue {
int count; Node *head; Node *tail; Queue(); bool empty(); int size(); bool enqueue(type); bool dequeue(type&); Node *find(type);
};
#endif
Node.cpp #include
Node::Node(type newItem) { item = newItem; next = NULL; }
Node.h #ifndef Node_type #define Node_type
using type = char;
struct Node { public: type item; Node *next; Node(type); };
#endif
Main.cpp #include
using namespace std;
int main() { char str[100] = { "The general furniture was profuse, comfortless, antique, and tattered." }; Queue q; cout
return 0;
} OUTPUT
E' DNuccd102aymay20181assg-test pebuglassg-testexe There are 8 spaces in this sentence Sentence after remoued space: Thegeneralfurniturewasprofuse,comfortless,antique, andtattered Press any key to continue . . . _Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started