Question: Note: Do not write in Java because my teacher accidentally writes it in Java and that it is not the correct answer. I want the
Note: Do not write in Java because my teacher accidentally writes it in Java and that it is not the correct answer. I want the coding in C++ because I never learn java. Please make sure to writing c++ make sure to write in different files like creating LinkList.h and PairList Class due to this class be a CSIS 211.
Task 1 - The Linked List
The linked list is at the core of most data structures. We see it in lists, stacks, queues and trees to name a few.
From the lecture material create a linked list class. This class should be generic so that it will operate on any given type. In C++ we call this a template class. In Java it is a Generic. Of course in Java a generic will not operate on a primitive type but that should be ok for what we are doing
Your class should have the following functionality:
- void addToEnd(T data) - adds an item to the end of the list
- void insert(T search, T data) - Adds data as a new Node following the node that contains search
- deleteNode(T search) - Deletes the node that contains search.
- void printList() - Prints the entire linked list.
- bool contains(T search) - returns true if the list contains T and false if it doesn't
If you find there is other functionality that you feel is needed to make your list better then by all means feel free to add it.
Task 2 The PairList Class
Using the PairList class you created in Assignment 8 remove the vector and replace it with the LinkedList you created in Task 1 above. This means that your PairList should now be derived from LinkedList and not vector.
Finally
Make sure you provide code to test your functionality thoroughly. In addition, make sure you are documenting everything. This means all methods and main with some instructions on how to demonstrate the use of your code.
Here is the coding my teacher help me but please turn the coding into multiple files. when doing this assignment?
#include#include using namespace std; // F first S second template class Pair { private: F first; S second; public: Pair(F first, S second) { this->first = first; this->second = second; } F getFirst() { return first; } S getSecond() { return second; } // mutators void setFirst(F first) { this->first = first; } void setSecond(S second) { this->second = second; } }; // pair list // first and second both are type T template class PairList { private: vector > list; public: // add pair value void addpair(T first, T second) { list.push_back(Pair (first, second)); } // find first by second T getFirst(T second) { // found first entry in case if duplicate exists // linear search T first; for(Pair p : list) { if(p.getSecond() == second) { first = p.getFirst(); break; } } return first; } // find second by first T getSecond(T first) { // found first entry in case if duplicate exists // linear search T second; for(Pair p : list) { if(p.getFirst() == first) { second = p.getSecond(); break; } } return second; } // deletes pair if exists void deletePair(T first, T second) { // found first entry in case if duplicate exists for(auto i = list.begin(); i != list.end(); ++i) { Pair p = *i; if(p.getFirst() == first && p.getSecond() == second) { // found first entry list.erase(i); break; } } } // print pair list void printList() { for(Pair p : list) { cout << "<" << p.getFirst() << "," << p.getSecond() << "> "; } cout << " "; } }; // driver program to test int main() { PairList plist; plist.addpair(10, 20); plist.addpair(15, 25); plist.addpair(17, 21); cout << plist.getFirst(20) << " "; cout << plist.getSecond(15) << " "; plist.printList(); plist.deletePair(15, 25); plist.printList(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
