C++ Please write the whole code that is needed for this assignment, write the completed code together with the provided code templates, make sure that all finished code files are compatible with each other, and present the needed output that is given in the output.txt file. Make sure to prove that by your output screenshot, don't copy anyone else's code, label in what file each code belongs, and please don't forget to share your code output after finishing writing the code, if these requirements aren't met, I will nor upvote the answer, everything must be clear and complete, everything is given below, including the needed output that you need to present, at the end of your code. Thank you.
ListNode.h
#include #include template
//template class ListNode{ public: T data; public: ListNode * next;
// post: constructs a node with data 0 and null link public: ListNode(); public: ListNode(T data); public: ListNode(T idata, ListNode * newNext); public:std::string toString(); public: ListNode * getNext(); public: void setNext(ListNode * newNext); public: T getData(); public: void setData(T newData); };
ListNodecpp.h
#include "ListNode.h" template // post: constructs a node with data 0 and null link //GIVEN ListNode:: ListNode() { //std::cout ListNode:: ListNode(T idata) { data=idata; next=nullptr; } //TODO template ListNode:: ListNode(T idata, ListNode* inext) { //TODO - assign data and next pointer } //GIVEN template std::string ListNode::toString(){ return data.toString(); } //GIVEN template ListNode * ListNode::getNext(){ return next; } //TODO template void ListNode::setNext(ListNode * newNext){ //TODO set the next pointer to incoming value } //GIVEN template T ListNode::getData(){ return data; } //TODO template void ListNode::setData(T newData){ //TODO set the data to incoming value }
main.cpp
#include
#include "ListNodecpp.h" //Requires: integer value for searching, address of front //Effects: traverses the list node chain starting from front until the end comparing search value with listnode getData. Returns the original search value if found, if not adds +1 to indicate not found //Modifies: Nothing int search(ListNode * front, int value); //Requires: integer value for inserting, address of front //Effects: creates a new ListNode with value and inserts in proper position (increasing order)in the chain. If chain is empty, adds to the beginning //Modifies: front, if node is added at the beginning. //Also changes the next pointer of the previous node to point to the newly inserted list node. the next pointer of the newly inserted pointer points to what was the next of the previous node. This way both previous and current links are adjusted
//******** NOTE the use of & in passing pointer to front as parameter - Why do you think this is needed ?**********
void insert(ListNode * &front,int value); //Requires: integer value for adding, address of front //Effects:creates a new ListNode with value and inserts at the beginning //Modifies:front, if node is added at the beginning.
void addNode(ListNode * &front,int value);
//Requires: integer value for removing, address of front //Effects: removes a node, if list is empty or node not found, removes nothing. //Modifies: If the first node is removed, front is adjusted. // if removal is at the end or middle, makes sure all nececssary links are updated. void remove(ListNode* & front, int value); //Requires: address of front //Effects: prints data of each node, by traversing the chain starting from the front using next pointers. //Modifies: nothing void printList(ListNode * front);
//GIVEN int main() { // Add 3 Nodes to the chain of ListNodes /ote AddNode method appends to the end so this will be out of order // the order of the nodes is 1,2 , 4 //Create a daisy chain aka Linked List // ListNode * front = nullptr; printList(front); std::cout // insert the smaller value at correct position in the front of the chain and change front insert(front, 1); printList(front); std::cout
//GIVEN void printList(ListNode * front){ ListNode * current = front; while (current!=nullptr){ std::coutgetData()getNext(); } if (front ==nullptr) std::cout //GIVEN int search(ListNode * front,int value){ ListNode * current = front; while (current!=nullptr&& current->getData()!=value){ // std::coutgetData()getNext(); } if (current!= nullptr) return current->getData(); return value+1 ; // to indicate not found. The calling program checks if return value is the same as search value to know if its found; I was using *-1 but if search value is 0, then that does not work; }
//GIVEN void addNode(ListNode * & front ,int value){ ListNode * current = front; ListNode * temp = new ListNode(value); if (front ==nullptr) front=temp; else { while (current->getNext()!=nullptr){ // std::coutgetData()getNext(); } //ListNode * temp = new ListNode(value); current->setNext(temp); }
} //TODO void remove(ListNode * &front,int value){ //TODO } //TODO void insert(ListNode * &front,int value){
//TODO }
output.txt
EMPTY LIST ********************** 1 ********************** 1 2 ********************** 1 2 4 ********************** 1 2 3 4 ********************** 2 3 4 ********************** 1 2 3 4 ********************** 1 2 4 ********************** 1 2 ********************** 1 2 ********************** 1 ********************** EMPTY LIST ********************** 4 ********************** 1 4 **********************
This assignment and the next (\#5) involve design and development of a sequential non contiguous and dynamic datastructure called LinkedList. A linked list object is a container consisting of connected ListNode objects. As before, we are not going to use pre-fabricated classes from the c++ library, but construct the LinkedList ADT from scratch. The first step is construction and testing of the ListNode class. A ListNode object contains a data field and a pointer to the next ListNode object (note the recursive definition). \#This assignment requires you to 1. Read the Assignment 4 Notes 2. Watch the Assignment 4 Support video 3. Implement the following methods of the ListNode class -custom constructor -setters for next pointer and data 4. Implement the insert and remove method in the main program 5. Scan the given template to find the above //TODO and implement the code needed //TODO in ListNodecpp. h file public: ListNode(T idata, ListNode newNext); public: void setNext(ListNode newNext); public: void setData(T newData); \# The driver is given ListNodeMain.cpp is given to you that does the following tests 1. Declares a pointer called front to point to a ListNode of datatype integer 2. Constructs four ListNodes with data 1,2,4 and adds them to form a linkeo list. 3. Inserts ListNode with data 3 to the list 4. Removes node 1 and adds it back to test removing and adding the first element 5. Removes node 3 to test removing a middle node 6. Removes node 4 to test removing the last node 7. Attempt to remove a non existent node 8. Remove all existing nodes to empty the list 9. Insert node 4 and then node 1 to test if insertions preserve order 10. Print the list