Question: I need help writing a main program to test the functions provided in the linked list toolkit. Use C++ please. #ifndef LINKEDLIST_H #define LINKEDLIST_H #include

I need help writing a main program to test the functions provided in the linked list toolkit.

Use C++ please.

#ifndef LINKEDLIST_H #define LINKEDLIST_H

#include #include

template

struct Node { Item data; Node *link; }; template size_t listLength(Node* headPtr); // Precondition: headPtr is the head pointer of a linked list. // Postcondition: The value returned is the number of nodes in the linked // list. The list itself is unaltered. template void listHeadInsert(Node*& headPtr, const Item& entry); // Precondition: headPtr is the head pointer of a linked list. // Postcondition: A new node containing the given entry has been added at // the head of the linked list; headPtr now points to the // head of new, longer linked list.

template void listInsert(Node* previousPtr, const Item& entry); // Precondition: previousPtr points to a node in a linked list. // Postcondition: A new node containing the given entry has been added // after the node that previousPtr points to. template Node* listSearch(Node* headPtr, const Item& target); // Precondition: headPtr is the head pointer of a linked list. // Postcondition: The pointer returned points to the first node containing // the specified target in its data member. If there is no // such node, the null pointer is returned. The list itself // is unaltered. template Node* listLocate(Node* headPtr, SizeType position); // Precondition: headPtr is the head pointer of a list, and position > 0. // Postcondition: The pointer returned points to the node at the specified // position in the list. (The head node is position 1, the // next node is position 2, and so on.) If there is no // such position, then the null pointer is returned. The // list itself is unaltered. template void listHeadRemove(Node*& headPtr); // Precondition: headPtr is the head pointer of a linked list, with at // least one node. // Postcondition: The head node has been removed and returned to the heap; // headPtr is now the head pointer of the new, shorter // linked list. template void listRemove(Node* previousPtr); // Precondition: previousPtr points to a node in a linked list, and this // is not the tail node of the list. // Postcondition: The node after previousPtr has been removed. template void listClear(Node*& headPtr); // Precondition: headPtr is the head pointer of a linked list. // Postcondition: All nodes of the list have been returned to the heap, // and the headPtr is now nullptr. template void listCopy(const Node* sourcePtr, Node*& headPtr, Node*& tailPtr); // Precondition: sourcePtr is the head pointer of a linked list. // Postcondition: headPtr and tailPtr are the head and tail pointers for // a new list that contains the same items as the list // pointed to by sourcePtr. template void listPiece(const Node* startPtr, const Node* endPtr, Node*& headPtr, Node*& tailPtr); // Precondition: sourcePtr and endPtr are pointers to nodes on the same // linked list, with the sourcePtr node at or before the // endPtr node. // Postcondition: headPtr and tailPtr are the head and tail pointers for // a new linked list that contains the items from sourcePtr // to endPtr. The original list is unaltered. // listLength() Template Function //------------------------------------------------------------------------// template size_t listLength(Node* headPtr) { Node *cursor; size_t answer; answer = 0; for (cursor = headPtr; cursor != nullptr; cursor = cursor->link) answer++; return answer; } // listHeadInsert() Template Function //------------------------------------------------------------------------// template void listHeadInsert(Node*& headPtr, const Item& newItem) { Node *insertPtr; insertPtr = new Node; insertPtr->data = newItem; insertPtr->link = headPtr; headPtr = insertPtr; } // listInsert() Template Function //------------------------------------------------------------------------// template void listInsert(Node* previousPtr, const Item& newItem) { Node *insertPtr; insertPtr = new Node; insertPtr->data = newItem; insertPtr->link = previousPtr->link; previousPtr->link = insertPtr; } // listSearch() Template Function //------------------------------------------------------------------------// template Node* listSearch(Node* headPtr, const Item& target) { Node *cursor; for (cursor = headPtr; cursor != nullptr; cursor = cursor->link) if (cursor->data == target) return cursor; return nullptr; } // listLocate() Template Function //------------------------------------------------------------------------// template Node* listLocate(Node* headPtr, SizeType position) { Node *cursor; size_t i; assert(position > 0); cursor = headPtr; for (i = 1; (i != position) && (cursor != nullptr); i++) cursor = cursor->link; return cursor; } // listHeadRemove() Template Function //------------------------------------------------------------------------// template void listHeadRemove(Node*& headPtr) { Node *removePtr; removePtr = headPtr; headPtr = headPtr->link; delete removePtr; } // listRemove()Template Function //------------------------------------------------------------------------// template void listRemove(Node* previousPtr) { Node *removePtr; removePtr = previousPtr->link; previousPtr->link = removePtr->link; delete removePtr; } // listClear() Template Function //------------------------------------------------------------------------// template void listClear(Node*& headPtr) { while (headPtr != nullptr) listHeadRemove(headPtr); } // listCopy() Template Function //------------------------------------------------------------------------// template void listCopy(const Node* sourcePtr, Node*& headPtr, Node*& tailPtr) { headPtr = nullptr; tailPtr = nullptr; // Handle the case of the empty list if (sourcePtr == nullptr) return; // Make the head node for the newly created list, and put data in it listHeadInsert(headPtr, sourcePtr->data); tailPtr = headPtr; // Copy the rest of the nodes one at a time, // adding at the tail of the new list for (sourcePtr = sourcePtr->link; sourcePtr != nullptr; sourcePtr = sourcePtr->link) { listInsert(tailPtr, sourcePtr->data); tailPtr = tailPtr->link; } } // listPiece() Template Function //------------------------------------------------------------------------// template void listPiece(const Node* startPtr, const Node* endPtr, Node*& headPtr, Node*& tailPtr) { headPtr = nullptr; tailPtr = nullptr; // Handle the case of the empty list if (startPtr == nullptr) return; // Make the head node for the newly created list, and put data in it listHeadInsert(headPtr, startPtr->data); tailPtr = headPtr; if (startPtr == endPtr) return; // Copy the rest of the nodes one at a time, // adding at the tail of the new list for (startPtr = startPtr->link; startPtr != nullptr; startPtr = startPtr->link) { listInsert(tailPtr, startPtr->data); tailPtr = tailPtr->link; if (startPtr == endPtr) return; } } #endif /* LINKEDLIST_H */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!