Question
hi im working on an an assignment in c++ and really need help with task three. Efficient Range-Set Storage. You are to produce a class
hi im working on an an assignment in c++ and really need help with task three.
Efficient Range-Set Storage. You are to produce a class RangeSet that maintains a set of nonnegative integers. The storage and display of the set should be in terms of rangesa range is a group of consecutive integers.
Task 1 The assignment is to produce a class RangeSet that maintains a set of integers, with functions to update and display the set in terms of ranges. The data must be stored in a linked list using struct RNode. A skeleton for this is provided. The linked list must be stored in increasing order with one node per range. (I found it easier to have a dummy node, but its up to you.) Your class should initially have the following three public functions:
Default constructor
The mutator void addLonelyRange(int min, int max): This adds all integers from min to max inclusive to the set. If the range is empty or contains a negative, it should print a suitable error message and not change the list. Important: This function may assume that the added range does not overlap nor extend any existing ranges; and it does not have to do any error-checking thereof.
The accessor void dump( ): this prints out the set as a series of ranges. If there is only one integer in a range, only one number should be displayed. An endl should be printed. You may add private helper functions as needed. For example, RangeSet S; S.addLonelyRange(22,33); S.addLonelyRange(1,1); S.addLonelyRange(77,78); S.dump( ) should produce 1,22-33,77-78,
Task 2 Add the following public functions to your class:
The accessor bool isInSet(int val): this returns whether the value val is in the set or not
An equality tester (overloaded ==) to compare two RangeSets
A suitable destructor
Task 3 (Challenging!) Add the following two public functions to your class:
bool deleteValue(int val): This deletes the integer val from the set. For example, if the set starts with just the range 1:100 and we delete 14, then the linked list must now have two nodes, one with the range 1:13 and the other with the rnage 15:100. It returns whether val was found or not.
void addRange(int min, int max): This adds the integers from min to max inclusive to the set. Unlike the earlier add command, this may overlap or extend existing ranges. This function should leave the linked list in minimal form; that is, with the fewest possible ranges. For example, if the set starts with 1:3, 5:7 and 11:23 and we add 4 thru 9, the linked list must now have two nodes, one with 1:9 and the other with 11:23. Again print suitable error message if range is empty or contains a negative.
here is my code:
syntax checker.cpp #include using namespace std; #include "RangeSet.h" int main( ) { RangeSet S,T; S.addLonelyRange(1,1); S.addLonelyRange(22,33); S.addLonelyRange(77,78); S.dump( ); cout << endl; cout << boolalpha << S.isInSet(10) << ":" << S.isInSet(30) << endl; /*T.dump( ); // should print "Empty" cout << boolalpha << (S==T) << endl; // should be false T.addLonelyRange(22,33); T.addLonelyRange(77,78); T.addLonelyRange(1,1); T.dump( ); cout << boolalpha << (S==T) << endl; // should be true*/ }
RangeSet.h
#ifndef RANGE_SET_H #define RANGE_SET_H #include using namespace std; struct RNode { int start, end; RNode* next; RNode() : next(nullptr){} RNode(int s, int e):start(s), end(e), next(nullptr) {} }; class RangeSet { private: RNode *head; int getLength(); bool isEmpty(); public: RangeSet(); void addLonelyRange(int min, int max); void dump(); bool isInSet(int val); bool operator == (const RangeSet &rs) const; }; #endif
RangeSet.cpp
#include #include "RangeSet.h" using namespace std; RangeSet::RangeSet(){ head = new RNode(); } void RangeSet::addLonelyRange(int min, int max){ if(min < 0 || max < 0){ cout << "Range cant be negative." << endl; return; } if (min > max){ cout << "Empty set" << endl; return; } RNode *curr = head->next; if(curr == nullptr){ curr = new RNode(min, max); head->next = curr; return; } while(curr->next != nullptr){ if(min < curr->next->start){ RNode *insert = new RNode(min, max); insert->next = curr->next; curr->next = insert; return; } curr = curr->next; } curr->next = new RNode(min, max); } bool RangeSet::isEmpty(){ if(head->next == nullptr) return true; return false; } void RangeSet::dump(){ if(isEmpty()) cout << "set is empty" << endl; RNode *curr = head->next; while(curr != nullptr){ if(curr->start == curr->end) cout << curr->start << ","; else cout << curr->start << "-" << curr->end << ","; curr = curr->next; } cout << endl; } bool RangeSet::isInSet(int val){ RNode *curr = head->next; while(curr != nullptr){ if(val >= curr->start && val <= curr->end) return true; curr = curr->next; } return false; }
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