Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the software for a list class called LinkedList that is contained in LinkedList.cpp that meets the following requirements: The LinkedList.h file is provided. Do

Write the software for a list class called LinkedList that is contained in

LinkedList.cpp

that meets the following requirements:

The LinkedList.h file is provided. Do not change any of the interfaces

that are given in the

LinkedList.h file. You can add private member functions if you need to.

The data structure that

implements the list will be a linked list. Each element in the list will

implemented with type

Node as defined in the LinkedList.h file. Each element (Node) will be

dynamically allocated.

When a list is initially created, it will be an empty list. List positions

start at zero, where

zero is the first element in the list. When a memory allocation fails,

this is considered a fatal

error and you should output an error message and call exit(1). All other

error conditions should

use the ErrorCode return type and must not output an error message.

Consult the .h file for more

information on the ErrorCode type.

You are to implement all the functions that are defined in the

LinkedList.h file in the LinkedList.cpp file.

Here are the files that are provided to you:

LinkedList.h

LinkedList.cpp

Main.cpp

MyFileIO.h

MyFileIO.cpp

Currently, these files will compile and link together. Make sure they

compile in your project before you start

modifying LinkedList.cpp. Do not modifiy LinkedList.h, MyFileIO.h and

MyFileIO.cpp.

The main.cpp file has a function called testCases(). You might want to

comment out the testCases()

function while debugging. Start your testing/debugging with basic

functionality and work your way

to the more complicated functionality. After you have debugged your

software and it is in good working

order, then you should uncomment the testCases() function. If you are

doing the extra credit function,

then you should call testCases(true) and there are 57 test cases that you

should pass. If you are not

doing the extra credit, then you should call testCases(false) and there

are 41 test cases that you

should pass. If any of the test cases fail, you should fix the defect(s)

in the LinkedList.cpp file.

You may need to run a subset of the 57 tests, because some of the tests

could cause the program to crash.

The tests start with basic tests and move to more difficult complex tests.

The testCases() function will

output the results to the console and write the results to a file called

LLresults.txt that should be

located in your working directory.

The following are the requirements of the member functions that you need

to implement for this linked list class. The software must not have any

shallow copy issues and

any memory leaks. Perform all error checking.

int getListSize( ) const - returns the current number of elements in the

list.

bool isEmpty( ) const - returns true if the list is empty, otherwise

returns false.

void resetList() - empties the current list so there are no elements

contained.

void display(std::ostream & out) const - displays the list to the console.

ErrorCode insert(ListElement x, int n) - inserts element x to position n

and

returns an ErrorCode.

ErrorCode erase(int n) - erase the List element at position n and return

an ErrorCode.

ErrorCode getListElement(int posStart, int posEnd, ListElement rv[]) const

- gets the elements

from list position posStart to posEnd, inclusive, and puts them in the

array rv.

The function caller must ensure the rv array is large enough to handle the

returned elements.

getListElement() returns an ErrorCode. Example: to get the first 3

elements of the list

call getListElement(0, 2, rv);

ErrorCode move(int n, int m) - move the list element from position n to

position m

and return an ErrorCode.

For an integer list of 22, 11, 8, 0, -2, a move(1,3) function would result

in a list of 22, 8, 0, 11, -2. For the list 22, 11, 8, 0, -2, a call of

move(1,1)

is valid and would result in a list of 22, 11, 8, 0, -2 (no change).

When implementing the code, no list elements can be de-allocated (deleted)

or

allocated. This function can only be implemented using pointer

manipulation.

void reverse() - reverse the order of a list. For an integer list of 22,

11, 8, 0, -2,

a reverse() function would result in a list of -2, 0, 8, 11, 22.

When implementing the code, no list elements can be deleted (de-allocated)

or

created (allocated). This function can only be implemented using pointer

manipulation.

If a destructor, assignment operator, and a copy constructor need to be

implemented, then they should be provided.

Extra Credit

const LinkedList operator+(const LinkedList & x, const LinkedList & y) -

this is a non-member function operator overlaod of

the + operator where x + y would return a new list c. The definition of

the addition of list x and y is as follows:

List x contains x1,x2,x3,...,xn

List y contains y1,y2,y3,...,ym

If n < m then list c will contain x1,y1,x2,y2,...,xn,yn,y(n+1),...,ym

If n > m then list c will contain x1,y1,x2,y2,...,xm,ym,x(m+1),...,yn

If n = m then list c will contain x1,y1,x2,y2,...,xn,yn

int operator==(const ListElement & x, const ListElement & y) -

this is a non-member function operator overlaod of

the == operator where x == y would return a 1 if the lists are equal,

otherwise a 0. For two lists to be equal, they must have the same

size and all elements must appear the same number of times in each list

regardless of the position.

Examples:

List x contains 1,2,3,4,5

List y contains 1,2,3,4,5,6

These lists are not equal

List x contains 1,2,3,4,5

List y contains 1,2,3,4,5

These lists are equal

List x contains 1,2,3,4,5

List y contains 5,4,3,1,2

These lists are equal

List x contains 1,2,3,4,4

List y contains 5,4,3,1,2

These lists are not equal

List x contains 1,1,1,2,2

List y contains 1,2,1,2,1

These lists are equal

Hints on how to move forward with this program:

Start with the simplier functions first. Constructor, isEmpty(), insert(), and erase() were discussed extensively in class. Please review the class slides to assist you with this implementation. Also, for traversinga linked list, there is the function getPtr(int pos) that is on slide 38of the List class slides.This function/software will be key to building the more complexfunctionality that will be needed.

CODES: LinkedList.cpp

/*-- LinkedList.cpp-------------------------------------------------------

----

This file implements List member functions.

-------------------------------------------------------------------------

*/

#include

#include "LinkedList.h"

//--- Definition of class constructor

LinkedList::LinkedList()

{}

//--- Definition of class destructor

LinkedList::~LinkedList()

{}

//--- Definition of copy constructor

LinkedList::LinkedList(const LinkedList & origList)

{}

//--- Definition of assignment operator

const LinkedList & LinkedList::operator=(const LinkedList & rightHandSide)

{

return *this;

}

//--- Definition of isEmpty()

bool LinkedList::isEmpty() const

{

return true;

}

int LinkedList::getListSize() const

{

return 0;

}

//--- Definition of display()

void LinkedList::display(std::ostream & out) const

{}

//--- Definition of + operator

LinkedList operator+(const LinkedList & x, const LinkedList & y)

{

LinkedList c;

return c;

}

//--- Definition of == operator

int operator==(const LinkedList & x, const LinkedList & y)

{

return 1;

}

//--- Definition of resetList()

void LinkedList::resetList()

{}

//--- Definition of insert()

LinkedList::ErrorCode LinkedList::insert(ListElement item, int pos)

{

return NO_ERROR;

}

//--- Definition of erase()

LinkedList::ErrorCode LinkedList::erase(int pos)

{

return NO_ERROR;

}

LinkedList::ErrorCode LinkedList::move(int n, int m)

{

return NO_ERROR;

}

void LinkedList::reverse()

{}

//--- Definition of getListElement()

LinkedList::ErrorCode LinkedList::getListElement(int posStart, int posEnd,

ListElement rv[]) const

{

return NO_ERROR;

}

linkedlist.h

#include

#ifndef LINKED_LIST_H

#define LINKED_LIST_H

class LinkedList

{

public:

typedef int ListElement;

private:

/******** Data Members ********/

class Node

{

public:

ListElement data;

Node * next;

};

Node *first; // pointer to first element in linked list

int mySize; // current size of list

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 "LinkedList.h"

#include "MyFileIO.h"

bool compareArray(int array1[], int array2[], int count);

void testCases(bool extraCredit);

int main()

{

testCases(false); // for extra credit test put true, otherwise false

return 0;

}

void testCases(bool extraCredit)

{

std::string FILENAME = "LLresults.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;

}

MyFileO.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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+j Explain the litigation risks in international labor relations.

Answered: 1 week ago

Question

=+j What rules will apply to the process of negotiations?

Answered: 1 week ago