Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question
Please add the following to the code!!!
 
- Add destructor
- Add copy constructor
- Overload the assignment operator
- Overload the << operator to display set
- Add elements in sorted way
- Make search (logn)
 
 
 
#include <iostream>
#include<set>
using namespace std;

class Set{
private:
    int* arr;
    int count=0;
    int capacity=10;
public:
    Set(){ 
        arr = new int[capacity]; 
    };
    void add(int);
    bool search(int);
    Set union_set(const Set& set2);
    void display();
};
int main(){
    Set set1;
    Set set2;
    set1.add(5);    set1.add(3);    set1.add(2);    set1.add(4);
    set2.add(7);    set2.add(3);    set2.add(8);    set2.add(9);
    Set set3 = set1+ set2;
    cout << set3;
    
   //example of the set use from the std library. Set implemented using BST
    set<int> myset;
    set<int>::iterator it; // pointer to a node
    // set some initial values:
    
     myset.insert(30);    
     myset.insert(70);
     myset.insert(90);
     myset.insert(20);
     myset.insert(10);
    //it = myset.find(20);
    //myset.erase(it);
    //myset.erase(myset.find(40));
    std::cout << "myset contains:";
    for (it = myset.begin(); it != myset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}
void Set::add(int data){
    if (count >= capacity){
            //expend set
     }
    if (!search(data)){
       
        //Futute improvement: insert sorted - lots of shifting, so BST is more efficient
        arr[count++] = data;
    }
}
bool Set::search(int data){
    //Futute improvement: binary search but has to be sorted
    for (int i = 0; i < count; i++){
        if (arr[i] == data)
            return true;
    }
    return false;
}
Set Set::union_set(const Set& set2){
    Set newSet;
    for (int i = 0; i < count; i++)
        newSet.add(arr[i]);
    for (int i = 0; i < set2.count; i++){
        newSet.add(set2.arr[i]);
    }
    return newSet;
}
void Set::display(){
    for (int i = 0; i < count; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
}
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY