Write the body of the member functions and basic accessor functions. /* ________
ID: 3648991 • Letter: W
Question
Write the body of the member functions and basic accessor functions. /* __________________ / | LinkedList Class | __________________/ Purpose: Implements a List ADT using a dynamically allocated linked list to store the elements of the list. Design: This linked list implementation is designed to have a "sentinel" node at the end of the list. The data element of the sentinel node is left undefined. Error Handling: Whenever a function is given invalid parameter values the message "!-- ERROR : PANIC in LINKEDLIST!!" and maybe an explanation is displayed. The list should remains unchanged. If the function returns a value this value is undefined unless otherwise specified by the function description. */ #ifndef LINKED_LIST_H #define LINKED_LIST_H #include template class LinkedList { public: T m_data; // Data to be stored LinkedList* m_next; // Pointer to the next element in the list static T m_objerr; // Purpose: Default constructor // Postconditions: next pointer set to NULL // -INLINE- LinkedList() : m_next(NULL) {} // Purpose: Auxiliaty constructor, construct from parameters // useful when inserting elements // Postconditions: data and next pointer set to parameters // -INLINE- LinkedList(const T& x, LinkedList* p) : m_data(x), m_next(p) {} // -------- // ---- Big 3 Member Functions --- // -------- // Purpose: Destructor // IMPORTANT:: FOr didactic purposes, the destructor is left empty. // YOU are expected to implement function clear() to de-allocate the list ~LinkedList() {} // Purpose: performs a deep copy of the data from rhs into this linked list // Parameters: rhs is linked list to be copied // Returns: *this // Postconditions: this list contains same data values (in the same order) // as are in rhs; any memory previously used by this list has been // deallocated. LinkedList& operator= (const LinkedList& rhs); // Purpose: copy constructor // Parameters: cpy is the LinkedList that is to be copied // Postconditions: this list contains same data values (in same order) // as in cpy. LinkedList(const LinkedList& cpy); // -------- // ---- Basic Accessor Operations --- // -------- // Purpose: accessor function for the current # data values in the list // Returns: current size of the list int size() const; // Purpose: determines whether the list is empty // Returns: true if list is empty; otherwise, false bool isEmpty() const; // Purpose: Get a pointer to the first element node // Returns: pointer to the first node in the list; // returns NULL if the list is empty LinkedList* getFirstPtr(); // Purpose: accessor function for last element node // Returns: pointer to the last element's node in the list; // returns NULL if list is empty LinkedList* getLastPtr(); // Purpose: accessor function for a node in the list // Returns: pointer to the node at the i'th position in the list; // returns NULL if no such element exists. LinkedList* getAtPtr(int i); // Purpose: accessor function for the first element of the list // Returns: a copy of the first element of the list, T first(); // Purpose: accessor function for the last element of the list // Returns: a copy of the last element of the list, T last(); // Purpose: accessor function for an element of the list // Returns: a copy of the element at the i'th position in the list T at(int i);Explanation / Answer
public class LinkNode { String data; LinkNode next; public LinkNode(String item) { data = item; } } public class LinkedList { LinkNode head; public LinkedList(String item) { head = new LinkNode(item); } public void add(String item) { //pseudo code: while next isn't null, walk the list //once you reach the end, create a new LinkNode and add the item to it. Then //set the last LinkNode's next to this new LinkNode } } append method: public void append(LinkedList myNextNode) { LinkedList current = this; //Make a variable to store a pointer to this LinkedList while (current.next != NULL) { //While we're not at the last node of the LinkedList current = current.next; //Go further down the rabbit hole. } current.next = myNextNode; //Now we're at the end, so simply replace the NULL with another Linked List! return; //and we're done! }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.