Question
Task #2 is to implement all the Linked List functions of homework #2 with the underlying data structure of a vector container class. Previously for
Task #2 is to implement all the Linked List functions of homework #2 with
the underlying data structure
of a vector container class. Previously for homework #2, the LinkedList
class was implemented with a linked
memory data structure. The List1.h file is provided to you and this has
the data structure of
std::vector
already defined. The class name should remain LinkedList. You will create
a List1.cpp file with the
implementation using the vector as the data structure. You can use the
implementation from the
homework #2 answers for the basis of your implementation. Make the
appropriate changes to support
the new data structure as needed. Follow these rules.
1) No new data members can be added to the LinkedList class. The vector
list is already defined in the List1.h file.
2) No changes to the interface of any of the functions defined in List1.h
are allowed.
3) Adding private member functions to the LinkedList class is allowed if
you need them.
4) Any member function of the vector class can be used to complete this
task.
5) The answers from homeowrk #2 can be used where appropriate to complete
this task.
All test cases provided in the Main-list.cpp need to be executed. The
results are captured in the
Vector-results.txt file in the working directory. You will compile
MyFileIO.cpp, MyFileIO.h, List1.h,
List1.cpp, and Main-template.cpp together when the List1.cpp file is
complete.
The four files provided will not compile together until the List1.cpp file
is completed.
Hint: Before starting to write the List1.cpp functions, review the member
functions of the vector class
to formulate a plan for conversion. Use as much of the capability of the
vector class as you can.
/*-- List1.h -------------------------------------------------------------
-
This header file defines the data type List for processing lists.
Operations are:
Constructor
Destructor
Copy constructor
Assignment operator
isEmpty: Check if list is empty
resetList: Empties a list
insert: Insert an item
erase: Remove an item
getListSize: Get the size of the list
getListElement: get a range of list elements
move: move an element from one position to another
reverse: reverse the list
+ operator overload
== operator overload
display: Output the list
-------------------------------------------------------------------------
*/
#include
#include
#ifndef LIST1_H
#define LIST1_H
class LinkedList
{
public:
typedef int ListElement;
private:
/******** Data Members ********/
std::vector
public:
/******** Error Codes ********/
enum ErrorCode { ILLEGAL_LIST_POSITION = -1, NO_ERROR = 0 };
/******** Function Members ********/
/***** Class constructor *****/
/*------------------------------------------------------------------
----
Construct a List object.
Precondition: None.
Postcondition: An empty List object is constructed; first ==
nullptr and mySize is 0.
--------------------------------------------------------------------
---*/
LinkedList();
/***** Class destructor *****/
/*------------------------------------------------------------------
----
Destroys a List object.
Precondition: The life of a List object is over.
Postcondition: The memory dynamically allocated by the constructor
for the array pointed to by myArray has been returned to the heap.
--------------------------------------------------------------------
---*/
~LinkedList();
/***** Copy constructor *****/
/*------------------------------------------------------------------
----
Construct a copy of a List object.
Precondition: A copy of origList is needed; origList is a const
reference parameter.
Postcondition: A copy of origList has been constructed.
--------------------------------------------------------------------
---*/
LinkedList(const LinkedList & origList);
/***** Assignment operator *****/
/*------------------------------------------------------------------
----
Assign a copy of a List object to the current object.
Precondition: rightHandSide List is required.
Postcondition: A copy of rightHandSide has been assigned to this
object. A const reference to this list is returned.
--------------------------------------------------------------------
---*/
const LinkedList & operator=(const LinkedList & rightHandSide);
/***** isEmpty operation *****/
/*------------------------------------------------------------------
----
Assign a copy of a List object to the current object.
Precondition: A constructed list, either empty or with elements.
Postcondition : return true if empty, otherwise false.
--------------------------------------------------------------------
---*/
bool isEmpty() const;
/***** mutators *****/
/***** reset list operation *****/
/*------------------------------------------------------------------
----
empty the current list and deallocate all list elements.
Precondition: A constructed list, either empty or with elements.
Postcondition : an empty list (no element in the list), all elements
are deallocated
--------------------------------------------------------------------
---*/
void resetList();
/***** insert operation *****/
/*------------------------------------------------------------------
----
Insert item at pos position. pos 0 is the first element position in
the list
Precondition: A constructed list, either empty or with elements
Postcondition : inserted item into list at pos position
Returns ILLEGAL_LIST_POSITION for insert that is out of range of the
current list,
Otherwise return a NO_ERROR.
--------------------------------------------------------------------
---*/
ErrorCode insert(ListElement item, int pos);
/***** erase operation *****/
/*------------------------------------------------------------------
----
Erase item at pos position. pos 0 is the first element position in
the list.
Precondition: A constructed list, either empty or with elements
Postcondition : erased item at pos position
Returns ILLEGAL_LIST_POSITION for erase that is out of range of the
current list,
Otherwise return a NO_ERROR.
--------------------------------------------------------------------
---*/
ErrorCode erase(int pos);
/***** move operation *****/
/*------------------------------------------------------------------
----
Move item from position n to position m
Precondition: A constructed list, either empty or with elements
Postcondition : item is moved from n to m position
Returns ILLEGAL_LIST_POSITION for move that is out of range of the
current list,
Otherwise return a NO_ERROR.
--------------------------------------------------------------------
---*/
ErrorCode move(int n, int m);
/***** reverse operation *****/
/*------------------------------------------------------------------
----
Reverse items in a list
Precondition: A constructed list, either empty or with elements.
Postcondition : List items are now in reverse order.
--------------------------------------------------------------------
---*/
void reverse();
// Accessors
/***** getListElement *****/
/*------------------------------------------------------------------
----
Returns a list element at position pos
Precondition: A constructed list, either empty or with elements.
The rv[] array must be large enough to hold the returned contents.
Postcondition : Fills array rv with the list elements specified
Returns ILLEGAL_LIST_POSITION for move that is out of range of the
current list,
Otherwise return a NO_ERROR. Both posStart and posEnd must be valid
positions
and posStart <= posEnd. posStart is an index to the start of the
data and
posEnd is an index to the end of the data. To retieve one element
posStart and posEnd will be the same value.
--------------------------------------------------------------------
---*/
ErrorCode getListElement(int posStart, int posEnd, ListElement rv[])
const;
/***** getListSize *****/
/*------------------------------------------------------------------
----
Returns the list size
Precondition: A constructed list, either empty or with elements.
Postcondition : Returns the list size
--------------------------------------------------------------------
---*/
int getListSize() const;
/***** output *****/
/*------------------------------------------------------------------
----
Display items in a list
Precondition: open output stream, A constructed list, either empty
or with elements.
Postcondition : items in list are displayed to console
--------------------------------------------------------------------
---*/
void display(std::ostream & out) const;
}; //--- end of List class
/***** operator + overload *****/
/*----------------------------------------------------------------------
define the + operator
Precondition: two lists x and y
Postcondition : return a merged addition list
-----------------------------------------------------------------------*/
LinkedList operator+(const LinkedList & x, const LinkedList & y);
/***** operator == overload *****/
/*----------------------------------------------------------------------
define the == operator
Precondition: two lists x and y
Postcondition : return of 1 if true, otherwise false
-----------------------------------------------------------------------*/
int operator==(const LinkedList & x, const LinkedList & y);
#endif
==============================================================================================================================
Main.cpp
==============================================================================================================================
#include
#include
#include
#include "List1.h"
#include "MyFileIO.h"
#include
bool compareArray(int array1[], int array2[], int count);
void testCases(bool extraCredit);
int main()
{
testCases(true); // for extra credit test put true, otherwise false
return 0;
}
void testCases(bool extraCredit)
{
std::string FILENAME = "Vector-results.txt";
std::string outString;
std::string PASSED = " Passed";
std::string FAILED = " Failed";
std::string TEST_CASE = "Test Case #";
int array[50]; // holds max list size I need for testing
int array1[] = { 11, 22, 33, 44 }; // pattern used for testing
LinkedList::ErrorCode tempInt;
LinkedList empty1; // empty list
LinkedList a; // list a used for testing
int temp8;
int testCount = 1; // used to track test numbers
bool temp;
char ch[100000]; // array large enough for the file output
int char_ptr = 0; // file index pointer
// Test Case #1 - test isEmpty
temp = empty1.isEmpty();
if (temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #2 - check empty list with getListSize()
int size = a.getListSize();
if (size == 0)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #3 - test insert() and isEmpty() with one element
tempInt = a.insert(11, 0); // insert at first position
temp = a.isEmpty();
if (temp == false && tempInt == LinkedList::NO_ERROR)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #4 - test getListSize() with one element
size = a.getListSize();
if (size == 1)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #5 - test that the one element is correct
tempInt = a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 1) == true && tempInt ==
LinkedList::NO_ERROR)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #6 - test resetList from a non-empty list
a.resetList();
temp = a.isEmpty();
if (temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #7 - add 2 element to the beginning position
a.resetList();
a.insert(22, 0); // insert at first position
a.insert(11, 0); // insert at first position
size = a.getListSize();
if (size == 2)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #8 - test that the elements are correct
tempInt = a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 2) == true && tempInt ==
LinkedList::NO_ERROR)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #9 - test that the list can be emptied with 2 elements
in it
a.resetList();
temp = a.isEmpty();
if (temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #10 - insert from the beginning and last position
a.resetList();
a.insert(22, 0); // insert at first position
a.insert(11, 0); // insert at first position
a.insert(33, 2); // insert at last position
size = a.getListSize();
if (size == 3)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #11 - test that the 3 elements are correct
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 3) == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #12 - test that the 3 element list can be emptied
a.resetList();
temp = a.isEmpty();
if (temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #13 - insert elements to the front end and middle
a.resetList();
a.insert(22, 0); // insert at first position
a.insert(11, 0); // insert at first position
a.insert(44, 2); // insert at last position
a.insert(33, 2); // insert at middle position
size = a.getListSize();
if (size == 4)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #14 - test that the elements are correct
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 4) == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #15 - test the list can be emptied
a.resetList();
temp = a.isEmpty();
if (temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #16 - test an illegal insertion
a.resetList();
a.insert(22, 0); // insert at first position
a.insert(11, 0); // insert at first position
a.insert(44, 2); // insert at last position
a.insert(33, 2); // insert at middle position
tempInt = a.insert(99, -1); // insert in an illegal position
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 4) == true && tempInt ==
LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #17 - test an illegal insertion
tempInt = a.insert(99, 5); // insert in an illegal position
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 4) == true && tempInt ==
LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #18 - test an illegal erase
tempInt = a.erase(-1); // erase from an illegal position
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 4) == true && tempInt ==
LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #19 - test an illegal erase
tempInt = a.erase(4); // erase from an illegal position
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 4) == true && tempInt ==
LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #20 - test erase from an empty list
tempInt = empty1.erase(0); // erase from an empty list
temp = empty1.isEmpty();
if (temp == true && tempInt == LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #21 - test erase from the end of the list
tempInt = a.erase(3); // erase from back of list
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 3) == true && tempInt ==
LinkedList::NO_ERROR)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #22 - test erase from the middle of the list
tempInt = a.erase(1); // erase from middle of list
array1[1] = 33;
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 2) == true && tempInt ==
LinkedList::NO_ERROR)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #23 - test erase from the front of the list
tempInt = a.erase(0); // erase from front of list
array1[0] = 33;
a.getListElement(0, a.getListSize() - 1, array);
if (compareArray(array, array1, 1) == true && tempInt ==
LinkedList::NO_ERROR)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #24 test erasing the last element so the list is empty
tempInt = a.erase(0); // erase the last element
temp = a.isEmpty();
if (temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #25 copy constructor - test if the lists are equal
a.insert(33, 0); // insert at first position
a.insert(11, 0); // insert at first position
a.insert(44, 2); // insert at last position
a.insert(22, 1); // insert at middle position
LinkedList b(a); // use copy constructor
int array2[] = { 11, 22, 33, 44 };
b.getListElement(0, b.getListSize() - 1, array);
temp = compareArray(array, array2, 4);
if ((temp == true) && (b.getListSize() == 4))
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #26 copy constructor - test shallow copy
b.insert(99, 1);
a.getListElement(1, 1, array);
temp8 = array[0];
if (temp8 == 22)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #27 assignment operator - test that the lists are equal
b.resetList();
b = a; // use assignment operator
b.getListElement(0, b.getListSize() - 1, array);
temp = compareArray(array, array2, 4);
if ((temp == true) && (b.getListSize() == 4))
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #28 assignment operator - shallow copy test
b.insert(99, 1);
a.getListElement(1, 1, array);
temp8 = array[0];
if (temp8 == 22)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #29 - test reverse() with empty list
empty1.reverse();
temp = empty1.isEmpty();
if (temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #30 - test reverse() with even number of elements
a.reverse(); // here a has 11, 22,33,44
a.getListElement(0, a.getListSize() - 1, array);
int array3[] = { 44, 33, 22, 11 };
temp = compareArray(array, array3, 4);
if ((temp == true) && (a.getListSize() == 4))
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #31 - test reverse() with odd number of elements
a.insert(55, 0);
a.reverse();
a.getListElement(0, a.getListSize() - 1, array);
int array4[] = { 11, 22, 33, 44, 55 };
temp = compareArray(array, array4, 5);
if ((temp == true) && (a.getListSize() == 5))
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #32 - test reverse() with 1 element
empty1.insert(55, 0);
empty1.reverse();
empty1.getListElement(0, empty1.getListSize() - 1, array);
array4[0] = 55;
temp = compareArray(array, array4, 1);
if ((temp == true) && (empty1.getListSize() == 1))
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #33 - test getListElement() for illegal positions
empty1.resetList();
tempInt = empty1.getListElement(-1, 1, array);
if (tempInt == LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #34 - test getListElement() for illegal positions
empty1.resetList();
tempInt = a.getListElement(1, 0, array);
if (tempInt == LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #35 - test getListElement() for illegal positions
empty1.resetList();
tempInt = a.getListElement(1, 5, array); // a has 5 elements now
if (tempInt == LinkedList::ILLEGAL_LIST_POSITION)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #36 - test move() from first to last position
empty1.resetList();
a.resetList();
a.insert(44, 0);
a.insert(11, 1);
a.insert(22, 2);
a.insert(33, 3);
tempInt = a.move(0, 3); // move first to last
a.getListElement(0, a.getListSize() - 1, array);
int array5[] = { 11, 22, 33, 44 };
temp = compareArray(array, array5, 4);
if (tempInt == LinkedList::NO_ERROR && temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #37 - test move() from last to first position
a.resetList();
a.insert(22, 0);
a.insert(33, 1);
a.insert(44, 2);
a.insert(11, 3);
tempInt = a.move(3, 0); // move last to first
a.getListElement(0, a.getListSize() - 1, array);
temp = compareArray(array, array5, 4);
if (tempInt == LinkedList::NO_ERROR && temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #38 - test move() from first to middle position
a.resetList();
a.insert(33, 0);
a.insert(11, 1);
a.insert(22, 2);
a.insert(44, 3);
tempInt = a.move(0, 2); // move first to middle
a.getListElement(0, a.getListSize() - 1, array);
temp = compareArray(array, array5, 4);
if (tempInt == LinkedList::NO_ERROR && temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #39 move() - test move() from first to middle position
a.resetList();
a.insert(11, 0);
a.insert(44, 1);
a.insert(22, 2);
a.insert(33, 3);
tempInt = a.move(1, 3); // move first to middle
a.getListElement(0, a.getListSize() - 1, array);
temp = compareArray(array, array5, 4);
if (tempInt == LinkedList::NO_ERROR && temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #40 - test move() error conditions
a.resetList();
a.insert(11, 0);
a.insert(22, 1);
a.insert(33, 2);
a.insert(44, 3);
tempInt = a.move(-1, 3); // move first to middle
a.getListElement(0, a.getListSize() - 1, array);
temp = compareArray(array, array5, 4);
if (tempInt == LinkedList::ILLEGAL_LIST_POSITION && temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #41 move() - test move() error conditions
a.resetList();
a.insert(11, 0);
a.insert(22, 1);
a.insert(33, 2);
a.insert(44, 3);
tempInt = a.move(0, 4); // move first to middle
a.getListElement(0, a.getListSize() - 1, array);
temp = compareArray(array, array5, 4);
if (tempInt == LinkedList::ILLEGAL_LIST_POSITION && temp == true)
outString = TEST_CASE + valueOf(testCount) + PASSED +
getCRLF();
else
outString = TEST_CASE + valueOf(testCount) + FAILED +
getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
if (extraCredit == true)
{
// Test Case #42 + operator - 2 empty lists
a.resetList();
b.resetList();
LinkedList c = a + b;
c.getListElement(0, c.getListSize() - 1, array);
temp = c.isEmpty();
if (temp == true && c.getListSize() == 0)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #43 + operator - 2 lists with 1 element
a.resetList();
a.insert(11, 0);
b.resetList();
b.insert(-11, 0);
c = a + b;
c.getListElement(0, c.getListSize() - 1, array);
int array6[] = { 11, -11 };
temp = compareArray(array, array6, 2);
if (temp == true && c.getListSize() == 2)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #44 + operator - 2 lists with 1 element and
empty
a.resetList();
a.insert(11, 0);
b.resetList();
c = a + b;
c.getListElement(0, c.getListSize() - 1, array);
temp = compareArray(array, array6, 1);
if (temp == true && c.getListSize() == 1)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #45 + operator - 2 lists, empty and 1
element
a.resetList();
b.resetList();
a.insert(11, 0);
c = a + b;
c.getListElement(0, c.getListSize() - 1, array);
temp = compareArray(array, array6, 1);
if (temp == true && c.getListSize() == 1)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #46 + operator - 2 lists with equal lengths
of 3 elements each
a.resetList();
a.insert(11, 0);
a.insert(22, 1);
a.insert(33, 2);
b.resetList();
b.insert(-11, 0);
b.insert(-22, 1);
b.insert(-33, 2);
c = a + b;
c.getListElement(0, c.getListSize() - 1, array);
int array7[] = { 11, -11, 22, -22, 33, -33 };
temp = compareArray(array, array7, 6);
if (temp == true && c.getListSize() == 6)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #47 + operator - 2 lists where a has 5
elements and b has 3
a.resetList();
a.insert(11, 0);
a.insert(22, 1);
a.insert(33, 2);
a.insert(44, 3);
a.insert(55, 4);
b.resetList();
b.insert(-11, 0);
b.insert(-22, 1);
b.insert(-33, 2);
c = a + b;
c.getListElement(0, c.getListSize() - 1, array);
int array8[] = { 11, -11, 22, -22, 33, -33, 44, 55 };
temp = compareArray(array, array8, 8);
if (temp == true && c.getListSize() == 8)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #48 + operator - 2 lists where b has 5
elements and a has 3
a.resetList();
a.insert(11, 0);
a.insert(22, 1);
a.insert(33, 2);
b.resetList();
b.insert(-11, 0);
b.insert(-22, 1);
b.insert(-33, 2);
b.insert(-44, 3);
b.insert(-55, 4);
c = a + b;
c.getListElement(0, c.getListSize() - 1, array);
int array9[] = { 11, -11, 22, -22, 33, -33, -44, -55 };
temp = compareArray(array, array9, 8);
if (temp == true && c.getListSize() == 8)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #49 == operator - test 2 empty lists
a.resetList();
b.resetList();
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #50 == operator - test 1 empty list and one
list with elements
a.resetList();
b.resetList();
b.insert(-11, 0);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #51 == operator - test 2 lists 11 and -11
a.resetList();
a.insert(11, 0);
b.resetList();
b.insert(-11, 0);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #52 == operator - test 2 lists 11 and 11
a.resetList();
a.insert(11, 0);
b.resetList();
b.insert(11, 0);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #53 == operator - test 2 lists 11,22,33,44
and 44,33,22,11
a.resetList();
a.insert(11, 0);
a.insert(22, 1);
a.insert(33, 2);
a.insert(44, 3);
b.resetList();
b.insert(44, 0);
b.insert(33, 1);
b.insert(22, 2);
b.insert(11, 3);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #54 == operator - test 2 lists 11,33,22,44
and 33,44,22,11
a.resetList();
a.insert(11, 0);
a.insert(33, 1);
a.insert(22, 2);
a.insert(44, 3);
b.resetList();
b.insert(33, 0);
b.insert(44, 1);
b.insert(22, 2);
b.insert(11, 3);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #56 == operator - test 2 lists 11,22,33,44
and 11,22,33,44,44
a.resetList();
a.insert(11, 0);
a.insert(22, 1);
a.insert(33, 2);
a.insert(44, 3);
b.resetList();
b.insert(11, 0);
b.insert(22, 1);
b.insert(33, 2);
b.insert(44, 3);
b.insert(44, 4);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #56 == operator - test lists 11,33,22,44 and
33,44,11,11
a.resetList();
a.insert(11, 0);
a.insert(33, 1);
a.insert(22, 2);
a.insert(44, 3);
b.resetList();
b.insert(33, 0);
b.insert(44, 1);
b.insert(11, 2);
b.insert(11, 3);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
// Test Case #57 == operator - test lists List x contains
1,1,1,2,2 and 1,2,1,2,1
a.resetList();
a.insert(1, 0);
a.insert(1, 1);
a.insert(1, 2);
a.insert(2, 3);
a.insert(2, 4);
b.resetList();
b.insert(1, 0);
b.insert(2, 1);
b.insert(1, 2);
b.insert(2, 3);
b.insert(1, 4);
if (a == b)
outString = TEST_CASE + valueOf(testCount) +
PASSED + getCRLF();
else
outString = TEST_CASE + valueOf(testCount) +
FAILED + getCRLF();
std::cout << outString;
char_ptr = build_file_array(outString, char_ptr, ch);
testCount++;
}
outString = getCRLF();
char_ptr = build_file_array(outString, char_ptr, ch);
outString = getCode(char_ptr, ch);
char_ptr = build_file_array(outString, char_ptr, ch);
outString = nowtoString();
char_ptr = build_file_array(outString, char_ptr, ch);
writefile(FILENAME, ch, char_ptr);
}
bool compareArray(int array1[], int array2[], int count)
{
bool rv = true;
for (int i = 0; i < count; i++)
{
if (array1[i] != array2[i])
rv = false;
}
return rv;
}
==============================================================================================================================
MyFileIO.cpp
==============================================================================================================================
#include
#include
#include
#include
#include
#include
#include "MyFileIO.h"
bool fileExists(std::string st)
{
bool result;
std::ifstream infile(st.c_str());
result = infile.good();
if (infile.is_open() == true)
{
infile.close();
}
return (result);
}
bool writefile(std::string filename, char *ptr, int length)
{
bool result = false;
std::ofstream outfile(filename.c_str(), std::ios::out |
std::ios::binary);
if (outfile.is_open() == true)
{
outfile.write(ptr, length);
outfile.close();
result = true;
}
return(result);
}
std::string nowtoString()
{
time_t now = time(0);
tm tstruct;
char buf[80];
localtime_s(&tstruct, &now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
std::string st(buf);
return st;
}
int build_file_array(std::string st, int ptr, char data[])
{
getChars(0, st.length(), data, ptr, st);
return (ptr + st.length());
}
void getChars(int beginst, int endst, char data[], int ptr, std::string
st)
{
int i;
for (i = 0; i < (endst - beginst); i++)
{
data[i + ptr] = st[beginst + i];
}
}
std::string valueOf(int num)
{
std::stringstream ss;
ss << num;
return(ss.str());
}
std::string getCRLF()
{
std::string CRLF = "\x0D\x0A";
return CRLF;
}
std::string getCode(int ptr, char data[])
{
int code = 0;
for (int i = 0; i < ptr; i++)
{
code = code + (int)data[i];
}
return (valueOf(code) + getCRLF());
}
==============================================================================================================================
MyFileIO.h
==============================================================================================================================
#ifndef MYFILEIO_H
#define MYFILEIO_H
#include
bool fileExists(std::string st);
bool writefile(std::string filename, char *ptr, int length);
std::string nowtoString();
void getChars(int beginst, int endst, char data[], int ptr, std::string
st);
int build_file_array(std::string st, int ptr, char data[]);
std::string valueOf(int num);
std::string getCRLF();
std::string getCode(int ptr, char data[]);
#endif
==============================================================================================================================
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