Question: You will create a program that runs in one of two modes, interactive mode and test mode. The mode will determined by the command line,

You will create a program that runs in one of two modes, interactive mode and test mode. The mode will determined by the command line, passing in a "-i" flag for interactive or "-t" for test mode. Require the user to pass in a flag.

$> ./lab02 -i Make a selection: 1) Insert value at position 2) Remove at position 3) Replace value at position 4) Print length 5) Print list 6) Exit Choice: 
$> ./lab02 -t  

LinkedList Header File

#ifndef LINKED_LIST_H #define LINKED_LIST_H #include "Node.h" //Gone over in class #include  //For runtime_error class LinkedList { private: Node* m_front; int m_length; public: LinkedList(); LinkedList(const LinkedList& original); ~LinkedList(); LinkedList& operator=(const LinkedList& original); bool isEmpty() const; int getLength() const; void insert(int position, int entry); void remove(int position); void clear(); int getEntry(int position) const; /** Here's an example of a doxygen comment block. Do this for all methods * @pre The position is between 1 and the list's length * @post The entry at the given position is replaced with the new entry * @param position: 1<= position <= length * @param newEntry: A new entry to put in the list * @throw std::runtime_error if the position is invalid. **/ void replace(int position, int newEntry); }; #endif 

Method Descriptions

  • LinkedList()
    • Creates an empty list
  • Copy Constructor
    • Creates a deep copy
  • ~LinkedList
    • deletes all nodes (note, this could just call clear...)
  • isEmpty
    • Returns true if empty, false otherwise
  • getLength
    • Returns the length
  • insert
    • adds a node containing the entry at that position (so the number of nodes is increased by 1)
    • positions range from 1 to length+1
    • NOTE: The reason for length+1 is to insert at the back of the list
    • if the position is out of range, it throws an exception
  • remove
    • deletes the node at that position (so the number of nodes is decreased by 1)
    • positions range from 1 to length
    • if the position is out of range, it throws an exception
  • clear
    • deletes all nodes in the list
  • getEntry
    • Returns the entry at a given position
    • positions range from 1 to length
    • if the position is out of range, it throws an exception
  • replace
    • replaces the entry at a given position
    • The number of nodes is unchanged
    • positions range from 1 to length
    • if the position is out of range, it throws an exception

Notes

  • You may add private helper functions as you see fit, but their responsibility should be in line with that of a List
  • Never expose private member to other scopes
  • LinkedLists ARE NOT in charge of printing themselves
  • Note: you'll also need to make a Node implementation

LinkedListTester class

  • Runs a battery of tests to verify that our Linked List is working
  • Has a single entry point called runTests()
  • Each test prints what test is currently running and if the List Passed or failed
  • Each test method should work in a vacuum, meaning each test

Sample LinkedListTester.h

class LinkedListTester { public: LinkedListTester(); //This will call all your test methods void runTests(); private: /** * @brief Creates an empty list and verifies isEmpty() returns true **/ void test01(); /** * @brief Creates an empty list adds 1 value, verifies isEmpty() returns false **/ void test02(); /** * @brief Creates an empty list and verifies getLength() returns 0 **/ void test03(); //more test methods as needed }; 

Tests

I am provided you with some starter tests that you must implement. You will also be required to add new tests for methods not mentioned here

Each test should be ran in isolation, meaning the tests could be run in any order and they don't share any objects/data.

Size tests

  1. size of empty list is zero
  2. size returns correct value after inserting at front of list
  3. size returns correct value after inserting at back of list
  4. size returns correct value after inserting in middle of list
  5. size returns correct value after adds and removing from front of list
  6. size returns correct value after adds and removing from back of list
  7. size returns correct value after adds and removing from middle of list

Insert tests

  1. insert throws exception if given an invalid position

Sample Test Output

$>./lab02 -t Test #1: size of empty list is zero PASSED Test #2: size returns correct value after inserting at front of list addFront PASSED Test #3: size returns correct value after inserting at back of list FAILED $> 

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!