Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include "Queue.h"

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 #include "Node.h"

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 #include #include "Queue.h"

using namespace std;

int main() { char str[100] = { "The general furniture was profuse, comfortless, antique, and tattered." }; Queue q; cout

return 0;

} OUTPUT

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions