Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
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;
#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;
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<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));
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';
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
}
//expend set
}
if (!search(data)){
//Futute improvement: insert sorted - lots of shifting, so BST is more efficient
arr[count++] = data;
}
//Futute improvement: insert sorted - lots of shifting, so BST is more efficient
arr[count++] = data;
}
}
bool Set::search(int 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;
}
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;
}
for (int i = 0; i < count; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 3 steps
Knowledge Booster
Similar questions
- #include #include using std::cout; using std::vector; class StatSet { private: HCreate a vector of doubles that is called "numbers." public: //Nothing to do in the constructor. StatSet() } // Add a new number to the set. vòid add_num(double num) { } double mean() { //Sum up the values and return the average. double sum = 0; //Write a loop that would iterate through the vector. sum += numbers[i]; return sum / numbers.size(); } double median() //Before calculating the median, we must sort the //array.arrow_forwardC++ Data Structure:Create an AVL Tree C++ class that works similarly to std::map, but does NOT use std::map. In the PRIVATE section, any members or methods may be modified in any way. Standard pointers or unique pointers may be used.** MUST use given Template below: #ifndef avltree_h#define avltree_h #include <memory> template <typename Key, typename Value=Key> class AVL_Tree { public: classNode { private: Key k; Value v; int bf; //balace factor std::unique_ptr<Node> left_, right_; Node(const Key& key) : k(key), bf(0) {} Node(const Key& key, const Value& value) : k(key), v(value), bf(0) {} public: Node *left() { return left_.get(); } Node *right() { return right_.get(); } const Key& key() const { return k; } const Value& value() const { return v; } const int balance_factor() const {…arrow_forwardArray_based circular queue: Define the class Queue using one dimensional circular array representation with no implementation; i.e. declare the data members, and the function members only (Enqueue, Dequeue, IsEmpty, GetHead etc.). Implement the Ennqueue method of the above classarrow_forward
- Student* func () { unique ptr arr[] make_unique ("CSC340") }; // #1 Insert Code int main () ( // #2 Insert Code [ #1 Insert Code]: Write code to keep all the object(s) which element(s) of array arr owns alive outside of the scope of func. [#2 Insert Code]: Write code to have a weak_ptr monitor the object which survived; Then test if it has any owner; Then properly destroy it; Then test again if has any owner; Then destroy the Control Block.arrow_forwardMy code from milestone 1 # Define the quiz_type dictionary quiz_type = { 1: "BabyAnimals", 2: "Brooklyn99", 3: "Disney", 4: "Hogwarts", 5: "MyersBriggs", 6: "SesameStreet", 7: "StarWars", 8: "Vegetables" } # Print the welcome message print("Welcome to the Personality Quiz!") print() print("What type of Personality Quiz do you want to run?") print() for number, quiz_name in quiz_type.items(): print(f"{number} - {quiz_name}") print() test_number = int(input("Choose test number (1-8): ")) # Check if the test_number is valid if test_number in quiz_type: quiz_name = quiz_type[test_number] print() print(f"Great! Let's begin the {quiz_name} Personality Quiz...") else: print("Invalid test number. Please choose a number between 1 and 8.") ------------------------------------------------------------------- Milestone #3 code : # Ending statements with the period to easily split them as mentioned in the question. questions = [ "I…arrow_forward#include <iostream>#include <vector>using namespace std; void PrintVectors(vector<int> numsList) { unsigned int i; for (i = 0; i < numsList.size(); ++i) { cout << numsList.at(i) << " "; } cout << endl;} int main() { vector<int> numsList; int userInput; int i; for (i = 0; i < 3; ++i) { cin >> userInput; numsList.push_back(userInput); } numsList.erase(numsList.begin()+1); numsList.insert(numsList.begin()+1, 102); numsList.insert(numsList.begin()+1, 100); PrintVectors(numsList); return 0;} Not all tests passed clearTesting with inputs: 33 200 10 Output differs. See highlights below. Special character legend Your output 33 100 102 10 Expected output 100 33 102 10 clearTesting with inputs: 6 7 8 Output differs. See highlights below. Special character legend Your output 6 100 102 8 Expected output 100 6 102 8 Not sure what I did wrong but the the 33…arrow_forward
- #include using namespace std; struct ListNode { string data; ListNode *next; }; int main() { ListNode *p, *list; list = new ListNode; list->data = "New York"; p new ListNode; p->data = "Boston"; list->next = p; p->next = new ListNode; p->next->data = "Houston"; p->next->next = nullptr; // new code goes here Which of the following code correctly deletes the node with value "Boston" from the list when added at point of insertion indicated above? O list->next = p; delete p; O p = list->next; %3D list->next = p->next; delete p; p = list->next; list = p->next; delete p; O None of these O p = list->next; %3D list->next = p; %3D delete p;arrow_forwardC programming fill in the following code #include "graph.h" #include <stdio.h>#include <stdlib.h> /* initialise an empty graph *//* return pointer to initialised graph */Graph *init_graph(void){} /* release memory for graph */void free_graph(Graph *graph){} /* initialise a vertex *//* return pointer to initialised vertex */Vertex *init_vertex(int id){} /* release memory for initialised vertex */void free_vertex(Vertex *vertex){} /* initialise an edge. *//* return pointer to initialised edge. */Edge *init_edge(void){} /* release memory for initialised edge. */void free_edge(Edge *edge){} /* remove all edges from vertex with id from to vertex with id to from graph. */void remove_edge(Graph *graph, int from, int to){} /* remove all edges from vertex with specified id. */void remove_edges(Graph *graph, int id){} /* output all vertices and edges in graph. *//* each vertex in the graphs should be printed on a new line *//* each vertex should be printed in the following format:…arrow_forwarddef distinct_last_name(full_names): ''' Question 5 You are given a large string that contains a list of full names separated by commas, you are asked to retrieve all distinct last names and store them in a set. Args: full_names (string) Returns: list >>> distinct_last_name('Harry Sharp, Chen Zhou, Tong Zhou, Dey Santanu') {'Sharp, Zhou, Santanu'} ''' # print(distinct_last_name('Harry Sharp, Chen Zhou, Tong Zhou, Dey Santanu'))arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY