Question
Download the files: LinkedList.h, LinkedList.cpp and LinkedListsMain.cpp. a. Create LinkedList class, which is similar to NumberList class discussed in lecture. Test your implementation using the
Download the files: LinkedList.h, LinkedList.cpp and LinkedListsMain.cpp. a. Create LinkedList class, which is similar to NumberList class discussed in lecture. Test your implementation using the driver program LinkedListsMain by creating an object of LinkedList class called myList b. Add the following functions to the basic LinkedList class you created in Problem 1.a: 1. reverseList(): reverses the order of the list. 2. locate(value): returns the position of the specific value in the linked list. The first node in the list is at position 0, the second node is at position 1, and so on. If the value is not found on the list, the search should return -1. 3. insertAt(node, pos): inserts a new node at the specified position. A position of 0 means that the value will become the first item on the list, a position of 1 means that the value will become the second item on the list, and so on. A position equal to or greater than the length of the list means that the value is placed at the end of the list. 4. removeFrom(node, pos): deletes a node at the specified position. A value of 0 for the position means that the first node in the list (the current head) is deleted. The function does nothing if the specified position is greater than or equal to the length of the list. Test your implementation through the driver program and object myList by calling the following functions: - appendNode(2); - appendNode(4); - appendNode(6); - insertAt(8, 0); - displayList(); - reverseList(); - displayList(); - removeFrom(99); - insertAt(10, -1); - cout << calling locate << locate(5) << endl; - removeFrom(1); - insertAt(7, 1); - displayList();
linkedlist.h
#ifndef LinkedList_H #define LinkedList_H class LinkedList { private: // TODO: Declare a structure for the list // TODO: Declare a List head pointer public: // Constructor LinkedList(); // Destructor ~LinkedList(); // Linked list operations void appendNode(double); void deleteNode(double); void displayList() const; }; #endif
linkedlist.cpp
// Implementation file for the LinkedList class #include#include "LinkedList.h" using namespace std; //************************************************** // constructor * // * //************************************************** LinkedList::LinkedList(){ //TODO: Make sure that the head node points // initially at NULL } //************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //************************************************** void LinkedList::appendNode(double num) { //TODO: create a node that point at the new node //TODO: create a node that will be used to // iterate through the list. // TODO: Allocate the new node and store num there. // TODO: Check if there are no nodes in the list // make newNode the first node. // Otherwise, insert newNode at end. // by finding the last node in the list. // then inserting newNode as the last node. if(){ } else{ while(){ } } } //************************************************** // The deleteNode function searches for a node * // with num as its value. The node, if found, is * // deleted from the list and from memory. * //************************************************** void LinkedList::deleteNode(double num) { //TODO: create a node that point at the previous node //TODO: create a node that will be used to // iterate through the list. // If the list is empty, do nothing. // Determine if the first node is the one, remove it. // else, initialize nodePtr to head of list and skip // all nodes whose value member is not equal to num. // If nodePtr is not at the end of the list, // link the previous node to the node after // nodePtr, then delete nodePtr. if(){ } else{ while(){ } if(){ } } } //************************************************** // displayList shows the value * // stored in each node of the linked list * // pointed to by head. * //************************************************** void LinkedList::displayList() const { //TODO: create a node that will be used to // iterate through the list. // TODO: Position nodePtr at the head of the list. // TODO: While nodePtr points to a node, traverse // the list and display the value in this node. // Remember to move to the next node. while(){ } } //************************************************** // Destructor * // This function deletes every node in the list. * //************************************************** LinkedList::~LinkedList() { //TODO: create a node that point at the next node //TODO: create a node that will be used to // iterate through the list. // TODO: Position nodePtr at the head of the list. // TODO: While nodePtr is not at the end of the list... // Save a pointer to the next node. // Delete the current node. // Position nodePtr at the next node. while(){ } }
linkedlistmain.cpp
#includeusing namespace std; int main() { // TODO: Define a LinkedList object. // TODO: Append some values to the list. // TODO: Display the contents of the list. return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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