Question: Data Structures. Modify the following two files to implement the functions in a linked list using recursion. This is done through Putty and Unix. The
Data Structures. Modify the following two files to implement the functions in a linked list using recursion. This is done through Putty and Unix. The two files that need to be modified are Node.hpp, where error checking is needed in appropriate functions that throw std::logic_error exceptions and exception_test_driver.cpp where cases will be tested. The functions that need to be completed are denoted by the bolded TODO comments in the code.
The first is Node.hpp
Node.hpp
/** * @file Node.hpp */
#ifndef NODE_HPP #define NODE_HPP
#include
/** * @class Node * @brief A node in a linked list */ template
/** * Insert an element into a linked list at a given position * @pre The position is within the bound of the list, that is, * 1 <= position <= length(head)+1 * @param head the head of the linked list. * @param position the list position at which to insert element. * @param element the element to insert into the list. * @return the head of the modified list */ template
// Base case if (position == 1) { Node
/** * Display a string representation of the list to standard out. * @param head the head of the linked list. */ template
/** * Checks whether the list is empty. * @param head the head of the linked list. * @return True if the list is empty, otherwise false. */ template
/** * Gets the number of elements in the list. * @param head the head of the linked list. * @return The length of the entries currently in the list. */ template
/** * Checks whether a element is contained in the list. * @param head the head of the linked list. * @param element the element to test. * @return True if the element is in the list, otherwise false. */ template
/** * Gets the element at the given position in the list * @pre 1 <= position <= length(head) * @param head the head of the linked list * @param position the list position of the desired element. * @return the element at the desired list position. */ template
/** * Gets the position of the first occurrence of the desired element. * @param head the head of the linked list * @param element the desired element. * @return The integer position of the desired element if it occurs in the list, * otherwise -1. */ template
/** * Replaces the element at the given position in the list. * @pre 1 <= position <= length(head) * @param head the head of the linked list. * @param position the list position of the element to replace. * @param element the replacement element * @return the head of the modified list. */ template
/** * Removes the element at a given position from the list. * @pre 1 <= position <= length(head) * @param head the head of the linked list. * @param position the list position of the entry to remove. * @return the head of the modified list. */ template
#endif
/** * @file Node.hpp */
#ifndef NODE_HPP #define NODE_HPP
#include
/** * @class Node * @brief A node in a linked list */ template
/** * Insert an element into a linked list at a given position * @pre The position is within the bound of the list, that is, * 1 <= position <= length(head)+1 * @param head the head of the linked list. * @param position the list position at which to insert element. * @param element the element to insert into the list. * @return the head of the modified list */ template
// Base case if (position == 1) { Node
/** * Display a string representation of the list to standard out. * @param head the head of the linked list. */ template
/** * Checks whether the list is empty. * @param head the head of the linked list. * @return True if the list is empty, otherwise false. */ template
/** * Gets the number of elements in the list. * @param head the head of the linked list. * @return The length of the entries currently in the list. */ template
/** * Checks whether a element is contained in the list. * @param head the head of the linked list. * @param element the element to test. * @return True if the element is in the list, otherwise false. */ template
/** * Gets the element at the given position in the list * @pre 1 <= position <= length(head) * @param head the head of the linked list * @param position the list position of the desired element. * @return the element at the desired list position. */ template
/** * Gets the position of the first occurrence of the desired element. * @param head the head of the linked list * @param element the desired element. * @return The integer position of the desired element if it occurs in the list, * otherwise -1. */ template
/** * Replaces the element at the given position in the list. * @pre 1 <= position <= length(head) * @param head the head of the linked list. * @param position the list position of the element to replace. * @param element the replacement element * @return the head of the modified list. */ template
/** * Removes the element at a given position from the list. * @pre 1 <= position <= length(head) * @param head the head of the linked list. * @param position the list position of the entry to remove. * @return the head of the modified list. */ template
#endif
-----------------------------------------------------------------------------------------------------------------------------------------------------------
exception_test_driver.cpp
/** * @file exception_test_driver.cpp */
#include "Node.hpp"
/** * Tests the error cases of the Node functions that rely on position. * @return the exit code */ int main() {
// this sets cout to print booleans as strings std::cout << std::boolalpha;
// TODO write tests that use invalid positions for the Node functions that // rely on position.
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
