In: Computer Science
LinkedList.h:
/*-- LinkedList.h --------------------------------------------------------------
This header file defines the data type List for processing
lists.
Operations are:
Constructor
Destructor
Copy constructor
Assignment operator
insert: Insert an item
erase: Remove an item
getListElement: get a range of list elements
-------------------------------------------------------------------------*/
#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 getListSize() const;
public:
/******** Error Codes ********/
static const int ILLEGAL_LIST_POSITION =
-1;
static const int NO_ERROR = 0;
LinkedList();
~LinkedList();
LinkedList(const LinkedList &
origList);
const LinkedList & operator=(const
LinkedList & rightHandSide);
int insert(ListElement item, int
pos);
int erase(int pos);
int getListElement(int posStart, int
posEnd, ListElement rv[]) const;
}; //--- end of List class
#endif
LinkedList.cpp:
/*-- LinkedList.cpp-----------------------------------------------------------
This file implements List member functions.
-------------------------------------------------------------------------*/
#include <new>
#include <iostream>
#include "LinkedList.h"
//--- Definition of class constructor
// Construct a List object.
// Precondition: None.
// Postcondition : An empty List object is constructed; first ==
nullptr
//--- Definition of class destructor
// Destroys a List object.
// Precondition : The life of a List object is over.
// Postcondition : The memory dynamically allocated to the linked
list is now deallocated
//--- Definition of 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.
//--- Definition of 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.
//--- Definition of insert()
// 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.
//--- Definition of erase()
// 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.
//--- Definition of getListElement()
// Returns list values
// 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.
int LinkedList::getListElement(int posStart, int posEnd,
ListElement rv[]) const
{
if (posStart < 0 || posStart >=
getListSize() || posEnd < 0 || posEnd >= getListSize() ||
posStart > posEnd)
{ // check for valid paramenters
return
ILLEGAL_LIST_POSITION;
}
Node *current = first; // point
to zero node
for (int i = 0; i < posStart; i++)
{ // loop to find the node
current =
current->next;
}
for (int i = 0; i < ((posEnd - posStart)
+ 1); i++)
{ // put returned elements in the beginning
of the list
rv[i] =
current->data;
current =
current->next;
}
return NO_ERROR;
}
//--- Definition of getSize()
// returns the size of the list. Do not change this.
int LinkedList::getListSize() const
{
int rv = 0;
Node *current = first;
for (; current != nullptr;)
{
rv++;
current =
current->next;
}
return rv;
}
LinkedList.h:
#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 getListSize() const;
public:
/******** Error Codes ********/
static const int ILLEGAL_LIST_POSITION = -1;
static const int NO_ERROR = 0;
LinkedList();
~LinkedList();
LinkedList(const LinkedList & origList);
const LinkedList & operator=(const LinkedList & rightHandSide);
int insert(ListElement item, int pos);
int erase(int pos);
int getListElement(int posStart, int posEnd, ListElement rv[]) const;
}; //--- end of List class
#endif
LinkedList.cpp:
#include <new>
#include <iostream>
#include "LinkedList.h"
int LinkedList::getListElement(int posStart, int posEnd, ListElement rv[]) const
{
if (posStart < 0 || posStart >= getListSize() || posEnd < 0 || posEnd >= getListSize() || posStart > posEnd)
{ // check for valid paramenters
return ILLEGAL_LIST_POSITION;
}
Node *current = first; // point to zero node
for (int i = 0; i < posStart; i++)
{ // loop to find the node
current = current->next;
}
for (int i = 0; i < ((posEnd - posStart) + 1); i++)
{ // put returned elements in the beginning of the list
rv[i] = current->data;
current = current->next;
}
return NO_ERROR;
}
int LinkedList::getListSize() const
{
int rv = 0;
Node *current = first;
for (; current != nullptr;)
{
rv++;
current = current->next;
}
return rv;
}