Implement all the functions that are defined in the LinkedList.h file in the Lin
ID: 3754089 • Letter: I
Question
Implement all the functions that are defined in the LinkedList.h file in the LinkedList.cpp file.
/*-- LinkedList.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 <iostream>
#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
/*-- LinkedList.cpp-----------------------------------------------------------
This file implements List member functions.
-------------------------------------------------------------------------*/
#include <new>
#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;
}
Explanation / Answer
LinkedList.cpp : ---------------->>>>>>>>>>>>
/*-- LinkedList.cpp-----------------------------------------------------------
This file implements List member functions.
-------------------------------------------------------------------------*/
#include <new>
#define nullptr NULL
#include "LinkedList.h"
using namespace std;
//--- Definition of class constructor
LinkedList::LinkedList()
{
first = nullptr;
mySize = 0;
}
//--- Definition of class destructor
LinkedList::~LinkedList()
{
resetList();
}
//--- Definition of copy constructor
LinkedList::LinkedList(const LinkedList & origList)
{
first = nullptr;
mySize = 0;
*this = origList;
}
//--- Definition of assignment operator
const LinkedList & LinkedList::operator=(const LinkedList & rightHandSide)
{
if(this == &rightHandSide){
return *this;
}
resetList();
Node *rf = rightHandSide.first;
Node *prev = nullptr;
Node *nn;
while(rf != nullptr){
nn = new Node;
nn->data = rf->data;
nn->next = nullptr;
if(prev == nullptr){
first = nn;
}else{
prev->next = nn;
}
prev = nn;
rf = rf->next;
}
mySize = rightHandSide.mySize;
return *this;
}
//--- Definition of isEmpty()
bool LinkedList::isEmpty() const
{
return first == nullptr && mySize == 0;
}
int LinkedList::getListSize() const
{
return mySize;
}
//--- Definition of display()
void LinkedList::display(std::ostream & out) const
{
Node *d = first;
out << " [ ";
while(d->next != nullptr){
out << d->data << " -> ";
}
if(d != nullptr){
out << d->data ;
}
out << " ] ";
}
//--- Definition of + operator
LinkedList operator+(const LinkedList & x, const LinkedList & y)
{
LinkedList c;
c = x;
int rv[y.getListSize()];
y.getListElement(0,y.getListSize()-1,rv);
for(int i = 0;i<y.getListSize();i++){
c.insert(rv[i],c.getListSize()-1);
}
return c;
}
//--- Definition of == operator
int operator==(const LinkedList & x, const LinkedList & y)
{
if(x.getListSize() != y.getListSize()){
return -1;
}
int rv1[x.getListSize()],rv2[y.getListSize()];
x.getListElement(0,x.getListSize()-1,rv1);
y.getListElement(0,y.getListSize()-1,rv2);
for(int i = 0;i<x.getListSize();i++){
if(rv1[i] != rv2[i]){
return -1;
}
}
return 1;
}
//--- Definition of resetList()
void LinkedList::resetList()
{
if(first != nullptr){
Node *last = first;
while(last != nullptr){
first = last;
last = last->next;
delete first;
}
first = nullptr;
mySize = 0;
}
}
//--- Definition of insert()
LinkedList::ErrorCode LinkedList::insert(ListElement item, int pos)
{
if(pos < 0 && pos >= mySize){
return ILLEGAL_LIST_POSITION;
}
Node *n = new Node;
n->data = item;
n->next = nullptr;
mySize++;
if(pos == 0){
n->next = first;
first = n;
return NO_ERROR;
}
Node *in = first;
for(int i = 0;i<pos-1;i++){
in = in->next;
}
n->next = in->next;
in->next = n;
return NO_ERROR;
}
//--- Definition of erase()
LinkedList::ErrorCode LinkedList::erase(int pos)
{
if(pos < 0 && pos >= mySize){
return ILLEGAL_LIST_POSITION;
}
Node *n = first;
mySize--;
if(pos == 0){
first = first->next;
delete n;
return NO_ERROR;
}
Node *in = first;
for(int i = 0;i<pos-1;i++){
in = in->next;
}
n = in->next;
in->next = n->next;
delete n;
return NO_ERROR;
}
LinkedList::ErrorCode LinkedList::move(int n, int m)
{
if(n < 0 && n >= mySize && m < 0 && m >= mySize){
return ILLEGAL_LIST_POSITION;
}
Node *nd = nullptr;
Node *md = nullptr;
Node *in = first;
for(int i = 0;i<mySize;i++){
if(i == n){
nd = in;
}
if(i == m){
md = in;
}
in = in->next;
}
ListElement t = nd->data;
nd->data = md->data;
md->data = t;
return NO_ERROR;
}
void LinkedList::reverse()
{
ListElement rv[mySize];
getListElement(0,mySize-1,rv);
Node *in = first;
for(int i = mySize-1;i>=0;i--){
in->data = rv[i];
in = in->next;
}
}
//--- Definition of getListElement()
LinkedList::ErrorCode LinkedList::getListElement(int posStart, int posEnd, ListElement rv[]) const
{
if(posStart < 0 && posStart >= mySize && posEnd < 0 && posEnd >= mySize && posEnd < posStart){
return ILLEGAL_LIST_POSITION;
}
Node *in = first;
for(int i = 0;i<posStart;i++){
in = in->next;
}
for(int i = 0;i <= (posEnd - posStart);i++){
rv[i] = in->data;
in = in->next;
}
return NO_ERROR;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.