Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, Please i need urgent help with this C++ question. I posted it before but the answer i got don't quite seem to be right.

Hi, Please i need urgent help with this C++ question. I posted it before but the answer i got don't quite seem to be right. here is the question below, also i have attached the sample source code.

Task A Rolodex is a rotating file that is usually used to record business contact information. Cards are inserted in alphabetic order by name. You operate the Rolodex by rolling it forwards or backwards until you find the information you need (or find the place where a new card should be inserted). Your task is to create a class that simulates a Rolodex and use it to implement a word-sorting program. The program starts with an empty Rolodex, then reads words from standard input and adds a card for each word into the appropriate position to maintain overall sorted order. To find the correct position, the Rolodex is rolled either backwards or forwards from the current card. When all input has been processed, the program moves the Rolodex to the beginning and then outputs each word in order, one per line.

Level 1: Basic operation Complete the implementation of Rolodex so that all the methods work correctly. The insert methods should leave the Rolodex positioned so that the current item is the one just inserted.

Level 2: No duplicates Modify the program so that the command line option -d (no duplicates) causes a new item to be inserted only if the item is not already present in the Rolodex. For example, if the input consisted of the text far far far away then the output would contain only two lines away far

Level 3: Deletion Modify the program such that an input string that begins with a - causes that word (without the -) to be deleted from the Rolodex instead of inserted. For example, if the input is the quick black fox jumps -black brown over the lazy dog then the output would contain the word brown but not black. Deleting a word should be implemented by moving the Rolodex to the position where the word would normally be inserted, but then deleting the current item if it matches the word (if it does not match, no action is performed). You will need to add a method to the Rolodex class that deletes the current item and leaves the Rolodex positioned so that the current item is the one following the deleted item if there is a following item, or the item preceding the deleted item otherwise. Hint: as part of your solution, consider the case where the input word is a - (i.e., a dash).

Level 4: Report Modify the program so that if it is run with the command line option -r (report), it outputs a report of the Rolodex operations rather than the words themselves. The report should consist of the following integer values separated by spaces: the number of items inserted, the number of duplicate items omitted (expected to be 0 unless the -d option is in effect), the number of items deleted, the number of times the rotateForward operation was performed, and the number of times the rotateBackward operation was performed. For example, if the input consisted of the text the quick brown fox jumps over the lazy dog then the output should read 9 0 0 5 8.

Sample code

#include #include using namespace std; template class Rolodex { public: typedef T* iterator; struct RolodexItem { T value_; RolodexItem* next_; RolodexItem* prev_; }; /** * Creates a new empty Rolodex */ Rolodex() { sentinel = new RolodexItem; sentinel->next_ = sentinel; sentinel->prev_ = sentinel; current = sentinel; } /** * Returns true if the Rolodex is positioned before the first card. */ bool isBeforeFirst() { return(current == sentinel); // change as needed } /** * Returns true if the Rolodex is positioned after the last card. */ bool isAfterLast() { return(current == sentinel); // change as needed } /** * Rotates the Rolodex one step forwards. */ void rotateForward() { current = current->next_; return; } /** * Rotates the Rolodex one step backwards. */ void rotateBackward() { current = current->prev_; return; } /** * Returns the value of the current card. */ const T& currentValue() { return(current->value_); // replace both lines with real code! } /** * Inserts a new card after the current position and * positions the Rolodex at the newly inserted card. */ void insertAfterCurrent(const T& value) { current->next_->prev_ = new RolodexItem; current->next_->prev_->prev_ = current; current->next_->prev_->next_ = current->next_; to current->next_ = current->next_->prev_; current = current->next_; current->value_ = value; return; } /** * Inserts a new card before the current position and * positions the Rolodex at the newly inserted card. */ void insertBeforeCurrent(const T& value) { current->prev_->next_ = new RolodexItem; current->prev_->next_->next_ = current; current->prev_->next_->prev_ = current->prev_; current->prev_ = current->prev_->next_; current = current->prev_; current->value_ = value; return; } void roloDelete() { RolodexItem* position; current->prev_->next_ = current->next_; current->next_->prev_ = current->prev_; if(current->next_ != sentinel) { position = current->next_; } else { position = current->prev_; } delete current; current = position; return; } private: RolodexItem* sentinel; RolodexItem* current; };

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

Step: 3

blur-text-image

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

Formal SQL Tuning For Oracle Databases Practical Efficiency Efficient Practice

Authors: Leonid Nossov ,Hanno Ernst ,Victor Chupis

1st Edition

3662570564, 978-3662570562

More Books

Students also viewed these Databases questions