Hello, there I\'m having problems compiling all 57 tests that are created in the
ID: 3752161 • Letter: H
Question
Hello, there
I'm having problems compiling all 57 tests that are created in the main. I changed the code for program and implemented the functions in LinkedList.cpp but the program can't compile all 57 tests.The only file that can only be modified is the linkedlist.cpp for my assignment. It works perfectly in Xcode showing all 57 tests but in Visual studio it doesnt's compile all 57 tests only 26 tests and print the test results into file named Llresults.txt. Sadly, This program is going to be tested in visual studio. I appreciate your help if you could make changes to linkedList.cpp file so it can show and passed all 57 tests. I will give you a great rating.
here are my code files:
(1) LinkedList.cpp (only modified this code file, do not change the other code files)
#include <new>
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList() {
first = nullptr;
mySize == 0;
}
LinkedList::~LinkedList() {
Node *x = first;
while (x != NULL) {
Node *y = x;
x = x->next;
delete y;
}
}
LinkedList::LinkedList(const LinkedList &origList) {
int count = 0;
Node *x = origList.first;
while (x != NULL) {
insert(x->data, count++);
x = x->next;
}
}
const LinkedList & LinkedList::operator=(const LinkedList & rightHandSide) {
resetList();
int count = 0;
Node *x = rightHandSide.first;
while (x != NULL) {
insert(x->data, count++);
x = x->next;
}
return *this;
}
bool LinkedList::isEmpty() const {
return (mySize == 0);
}
int LinkedList::getListSize() const {
return mySize;
}
void LinkedList::display(std::ostream & out) const {
Node *x = first;
while (x != NULL) {
out << x->data << " ";
x = x->next;
}
out << " ";
}
LinkedList operator+(const LinkedList & x, const LinkedList & y) {
LinkedList c;
int xSize = x.getListSize();
int ySize = y.getListSize();
LinkedList::ListElement *data1 = new LinkedList::ListElement[xSize];
LinkedList::ListElement *data2 = new LinkedList::ListElement[ySize];
x.getListElement(0, xSize - 1, data1);
y.getListElement(0, ySize - 1, data2);
int i = 0, j = 0, count = 0;
while (i < xSize || j < ySize) {
if (i < xSize) {
c.insert(data1[i], count++);
i++;
}
if (j < ySize) {
c.insert(data2[j], count++);
j++;
}
}
return c;
}
int operator==(const LinkedList & x, const LinkedList & y) {
int xSize = x.getListSize();
int ySize = y.getListSize();
if (xSize != ySize) {
return 0;
}
LinkedList::ListElement *data1 = new LinkedList::ListElement[xSize];
LinkedList::ListElement *data2 = new LinkedList::ListElement[ySize];
x.getListElement(0, xSize - 1, data1);
y.getListElement(0, ySize - 1, data2);
for (int i = 0; i<xSize; i++) {
LinkedList::ListElement d = data1[i];
bool found = false;
for (int j = 0; j<ySize; j++) {
if (d == data2[j]) {
found = true;
break;
}
}
if (!found) {
return 0;
}
}
for (int i = 0; i<ySize; i++) {
LinkedList::ListElement d = data2[i];
bool found = false;
for (int j = 0; j<xSize; j++) {
if (d == data1[j]) {
found = true;
break;
}
}
if (!found) {
return 0;
}
}
return 1;
}
void LinkedList::resetList() {
Node *x = first;
while (x != NULL) {
Node *y = x;
x = x->next;
delete y;
}
first = NULL;
mySize = 0;
}
LinkedList::ErrorCode LinkedList::insert(ListElement item, int pos) {
int size = getListSize();
if (pos < 0 || pos > size) {
return ILLEGAL_LIST_POSITION;
}
Node *x = new Node;
x->data = item;
if (pos == 0) {
x->next = first;
first = x;
}
else {
Node *y = first;
while (pos != 1) {
y = y->next;
pos--;
}
x->next = y->next;
y->next = x;
}
mySize++;
return NO_ERROR;
}
LinkedList::ErrorCode LinkedList::erase(int pos) {
int size = getListSize();
if (pos < 0 || pos >= size) {
return ILLEGAL_LIST_POSITION;
}
if (pos == 0) {
Node *y = first;
first = first->next;
delete y;
}
else {
Node *y = first;
while (pos != 1) {
y = y->next;
pos--;
}
Node *x = y->next;
y->next = y->next->next;
delete x;
}
mySize--;
return NO_ERROR;
}
LinkedList::ErrorCode LinkedList::move(int n, int m) {
int size = getListSize();
if (n < 0 || n >= size) {
return ILLEGAL_LIST_POSITION;
}
if (m < 0 || m >= size) {
return ILLEGAL_LIST_POSITION;
}
if (m != n) {
Node *a = first;
int count = 0;
while (a != NULL) {
if (n == count) {
break;
}
a = a->next;
count++;
}
ListElement temp = a->data;
erase(n);
insert(temp, m);
}
return NO_ERROR;
}
void LinkedList::reverse() {
Node *current = first;
Node *prev = NULL, *next = NULL;
while (current != NULL) {
next = current->next;
// Reverse current node's pointer
current->next = prev;
prev = current;
current = next;
}
first = prev;
}
LinkedList::ErrorCode LinkedList::getListElement(int posStart, int posEnd, ListElement rv[]) const {
int size = getListSize();
if (posStart < 0 || posStart >= size) {
return ILLEGAL_LIST_POSITION;
}
if (posEnd < 0 || posEnd >= size) {
return ILLEGAL_LIST_POSITION;
}
if (posEnd < posStart) {
return ILLEGAL_LIST_POSITION;
}
int index = 0;
Node *x, *y;
Node *a = first;
int count = 0;
while (a != NULL) {
if (posStart == count) {
x = a;
}
if (posEnd == count) {
y = a;
}
a = a->next;
count++;
}
a = x;
while (a != y->next) {
rv[index++] = a->data;
a = a->next;
}
return NO_ERROR;
}
(2) LinkedList.h
#include <iostream>
#include<string>
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
using namespace std;
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:
/******** Function Members ********/
enum ErrorCode { ILLEGAL_LIST_POSITION = -1, NO_ERROR = 0 };/******** Error Codes ********/
LinkedList();//--- Definition of class constructor
~LinkedList();//--- Definition of class destructor
LinkedList(const LinkedList & origList);//--- Definition of copy constructor
const LinkedList & operator=(const LinkedList & rightHandSide);//--- Definition of assignment operator
bool isEmpty() const;//--- Definition of isEmpty()
/***** mutators *****/
void resetList();//--- Definition of resetList()
ErrorCode insert(ListElement item, int pos);//--- Definition of insert()
ErrorCode erase(int pos);//--- Definition of erase()
ErrorCode move(int n, int m);//--Definition of move operation()
void reverse();//--Definition of reverse()
// Accessors
ErrorCode getListElement(int posStart, int posEnd, ListElement rv[]) const;//--- Definition of getListElement()
int getListSize() const;//--- Definition of getListSize()
void display(std::ostream & out) const;//--- Definition of display()
}; //--- end of List class
LinkedList operator+(const LinkedList & x, const LinkedList & y);//--- Definition of + operator
int operator==(const LinkedList & x, const LinkedList & y);//--- Definition of == operator
#endif
(3) MyFileIO.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <time.h>
#include "MyFileIO.h"
using namespace std;
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 = " ";
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());
}
(4) MyFileIO.h
#ifndef MYFILEIO_H
#define MYFILEIO_H
#include<iostream>
#include <string>
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
(5) Main.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "LinkedList.h"
#include "MyFileIO.h"
using namespace std;
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;
}
Here is my output in Xcode to show that it works but sadly this program needs to do the same in Visual Studio for my instructor.
here is my output in visual studio: (errors were fixed, and this program needs to execute all 57 tests in visual studio)
10 main.cpp 12 #ifndef LINKED-LIST-H 13 #define LINKED-LIST-H 14 15 16 class LinkedListt 17 18 public: 19 20 private: 21 LinkedList.cpp hLinkedList.h . MyFileio.cpp h MyFilelO.h Products typedef int ListElement; Data Members class Node public: 24 25 ListElement data; Node *next; Node first; I/ pointer to first element in linked list int mySize; /I current size of list 30 31 32 public: 34 35 36 iFunction Members enum ErrorCode { ILLEGAL-LIST-POSITION =-1, NO-ERROR = 0 };/ Error Codes / Test Case #44 Passed Test Case #45 Passed Test Case #46 Passed Test Case #47 Passed Test Case #48 Passed Test Case #49 Passed Test Case #5e Passed Test Case #51 Passed Test Case #52 Passed Test Case #53 Passed Test Case #54 Passed Test Case #55 Passed Test Case #56 Passed Test Case #57 Passed Program ended with exit code:e All Output ¢ O FilterExplanation / Answer
Can you please try my version of linkedlist.cpp:
#include <new>
#include "LinkedList.h"
LinkedList::LinkedList(){
first = NULL;
mySize = 0;
}
LinkedList::~LinkedList(){
Node *x = first;
while(x != NULL) {
Node *y = x;
x = x ->next;
delete y;
}
}
LinkedList::LinkedList(const LinkedList &origList){
first = NULL;
mySize = 0;
int count = 0;
Node *x = origList.first;
while(x != NULL) {
insert(x->data, count++);
x = x ->next;
}
}
const LinkedList & LinkedList::operator=(const LinkedList & rightHandSide){
resetList();
int count = 0;
Node *x = rightHandSide.first;
while(x != NULL) {
insert(x->data, count++);
x = x ->next;
}
return *this;
}
bool LinkedList::isEmpty() const{
return mySize == 0;
}
int LinkedList::getListSize() const{
return mySize;
}
void LinkedList::display(std::ostream & out) const{
Node *x = first;
while(x != NULL) {
out << x->data << " ";
x = x ->next;
}
out << " ";
}
LinkedList operator+(const LinkedList & x, const LinkedList & y){
LinkedList c;
int xSize = x.getListSize();
int ySize = y.getListSize();
LinkedList::ListElement *data1 = new LinkedList::ListElement[xSize];
LinkedList::ListElement *data2 = new LinkedList::ListElement[ySize];
x.getListElement(0, xSize-1, data1);
y.getListElement(0, ySize-1, data2);
int i=0, j=0, count = 0;
while(i < xSize || j < ySize) {
if(i < xSize) {
c.insert(data1[i], count++);
i++;
}
if(j < ySize) {
c.insert(data2[j], count++);
j++;
}
}
return c;
}
int operator==(const LinkedList & x, const LinkedList & y){
int xSize = x.getListSize();
int ySize = y.getListSize();
if(xSize != ySize) {
return 0;
}
LinkedList::ListElement *data1 = new LinkedList::ListElement[xSize];
LinkedList::ListElement *data2 = new LinkedList::ListElement[ySize];
x.getListElement(0, xSize-1, data1);
y.getListElement(0, ySize-1, data2);
for(int i=0; i<xSize; i++) {
LinkedList::ListElement d = data1[i];
bool found = false;
for(int j=0; j<ySize; j++) {
if(d == data2[j]) {
found = true;
break;
}
}
if(!found) {
return 0;
}
}
for(int i=0; i<ySize; i++) {
LinkedList::ListElement d = data2[i];
bool found = false;
for(int j=0; j<xSize; j++) {
if(d == data1[j]) {
found = true;
break;
}
}
if(!found) {
return 0;
}
}
return 1;
}
void LinkedList::resetList(){
Node *x = first;
while(x != NULL) {
Node *y = x->next;
delete x;
x = y;
}
first = NULL;
mySize = 0;
}
LinkedList::ErrorCode LinkedList::insert(ListElement item, int pos){
int size = getListSize();
if(pos < 0 || pos > size) {
return ILLEGAL_LIST_POSITION;
}
Node *x = new Node;
x->data = item;
x->next = NULL;
if(pos == 0) {
x->next = first;
first = x;
} else {
Node *y = first;
while(pos != 1) {
y = y ->next;
pos--;
}
x->next = y->next;
y->next = x;
}
mySize++;
return NO_ERROR;
}
LinkedList::ErrorCode LinkedList::erase(int pos){
int size = getListSize();
if(pos < 0 || pos >= size) {
return ILLEGAL_LIST_POSITION;
}
if(pos == 0) {
Node *y = first;
first = first->next;
delete y;
} else {
Node *y = first;
while(pos != 1) {
y = y ->next;
pos--;
}
Node *x = y->next;
y->next = y->next->next;
delete x;
}
mySize--;
return NO_ERROR;
}
LinkedList::ErrorCode LinkedList::move(int n, int m){
int size = getListSize();
if(n < 0 || n >= size) {
return ILLEGAL_LIST_POSITION;
}
if(m < 0 || m >= size) {
return ILLEGAL_LIST_POSITION;
}
if(m != n) {
Node *a = first;
int count = 0;
while(a != NULL) {
if(n == count) {
break;
}
a = a ->next;
count++;
}
ListElement temp = a->data;
erase(n);
insert(temp, m);
}
return NO_ERROR;
}
void LinkedList::reverse(){
Node *current = first;
Node *prev = NULL, *next = NULL;
while (current != NULL) {
next = current->next;
// Reverse current node's pointer
current->next = prev;
prev = current;
current = next;
}
first = prev;
}
LinkedList::ErrorCode LinkedList::getListElement(int posStart, int posEnd, ListElement rv[]) const {
int size = getListSize();
if(posStart < 0 || posStart >= size) {
return ILLEGAL_LIST_POSITION;
}
if(posEnd < 0 || posEnd >= size) {
return ILLEGAL_LIST_POSITION;
}
if(posEnd < posStart) {
return ILLEGAL_LIST_POSITION;
}
int index = 0;
Node *x, *y;
Node *a = first;
int count = 0;
while(a != NULL) {
if(posStart == count) {
x = a;
}
if(posEnd == count) {
y = a;
}
a = a ->next;
count++;
}
a = x;
while(a != y->next) {
rv[index++] = a->data;
a = a->next;
}
return NO_ERROR;
}
In case of any doubts, please ask in comments. If the answer helps you, please upvote. I am in great need of upvotes. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.