Here is a CPP class definition for an adt LinkedList of strings. Please Implement each member of the functions in the class below. You can
Here is a CPP class definition for an adt LinkedList of strings. Please Implement each member of the functions in the class below. You can add w/e private or public data members you want in the class. #include #include using namespace std; typedef string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private Node *head; public: // default constructor LinkedList() : head(nullptr) { } // copy constructor LinkedList(const LinkedList& rhs); // Destroys all the dynamically allocated memory // in the list. ~LinkedList(); // assignment operator const LinkedList& operator=(const LinkedList& rhs); ] // Inserts val at the front of the list void insertToFront(const ItemType &val); // Prints the LinkedList void printList() const; // Sets item to the value at position i in this // LinkedList and return true, returns false if // there is no element i bool get(int i, ItemType& item) const; // Reverses the LinkedList void reverseList(); // Prints the LinkedList in reverse order void printReverse() const; // Appends the values of other onto the end of this // LinkedList. void append(const LinkedList &other); // Exchange the contents of this LinkedList with the other one. void swap(LinkedList &other); // Returns the number of items in the Linked List. int size() const; }; When we don't want a function to change a parameter representing a value of the type stored in the LinkedList, we pass that parameter by constant reference. Passing it by value would have been perfectly fine for this problem, but we chose the const reference alternative because that will be more suitable after we make some generalizations in a later problem. The get function enables a client to iterate over all elements of a LinkedList. In other words, this code fragment LinkedList ls; ls.insertToFront("Jack"); ls.insertToFront("Germaine"); ls.insertToFront("Agatha"); ls.insertToFront("Agnes"); for (int k = 0; k < ls.size(); k++) { string x; ls.get(k, x); cout << x << endl; } must write Agnes Agatha Germaine Jack The printList and printReverse functions enables a client to print elements of a LinkedList. In other words, this code fragment LinkedList ls; ls.insertToFront("George"); ls.insertToFront("Louise"); ls.insertToFront("Lionel"); ls.insertToFront("Helen"); ls.printList(); ls.printReverse(); must write Helen Lionel Louise George George Louise Lionel Helen You should have one space between after each item printed with an additional newline after the last item. Here is an example of the append function: LinkedList e1; e1.insertToFront("bell"); e1.insertToFront("biv"); e1.insertToFront("devoe"); LinkedList e2; e2.insertToFront("Andre"); e2.insertToFront("Big Boi"); e1.append(e2); // adds contents of e2 to the end of e1 string s; assert(e1.size() == 5 && e1.get(3, s) && s == "Big Boi"); assert(e2.size() == 2 && e2.get(1, s) && s == "Andre"); Here is an example of the reverseList function: LinkedList e1; e1.insertToFront("Sam"); e1.insertToFront("Carla"); e1.insertToFront("Cliff"); e1.insertToFront("Norm"); e1.reverseList(); // reverses the contents of e1 string s; assert(e1.size() == 4 && e1.get(0, s) && s == "Sam"); ex. of the swap function: LinkedList e1; e1.insertToFront("A"); e1.insertToFront("B"); e1.insertToFront("C"); e1.insertToFront("D"); LinkedList e2; e2.insertToFront("X"); e2.insertToFront("Y"); e2.insertToFront("Z"); e1.swap(e2); // exchange contents of e1 and e2 string s; assert(e1.size() == 3 && e1.get(0, s) && s == "Z"); assert(e2.size() == 4 && e2.get(2, s) && s == "B"); When comparing items, just use the == or != operators provided for the string type by the library.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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