I have just started a homework problem which asks me to implement a linked list using a dynamic array.
The purpose of this assignment is to do the memory management of a singly linked list with a growing array. The C++ class manipulates two singly linked lists (one of free nodes and the other one of occupied nodes) but instead of having pointers that refer to addresses in memory, you are going to have indices of to a dynamic array and the elements of the array are going to be the “nodes” containing the value of the element and an index to the next node.
Here is my .h file
class LList {
public:
// create an empty list
LList();
// determine if the list is empty
bool isEmpty() const;
// insert x at the beginning of the list
// precondition: there is enough heap memory
// postcondition: x is placed in the list as first element
void cons(int x);
// append x to the end of the list
// precondition: there is enough heap memory
// postcondition: x is placed as the last element
void append(int x);
// return a string consisting of all the elements of the list
// in the order in which they are in the list from first to last in this format:
// -> the string starts with "[" followed by the LList::DELIMITER
// -> for each element: the element's value followed by the LList::DELIMITER
// -> the string finishes with "]"
// e.g. a string of 3 elements "[ 41 36 999 ]"
std::string toString() const;
// return the number of elements in the list
int length() const;
// search x in the list
// postcondition: return true if found, false oth
bool found(int x) const;
// return the first element of the list and LList::NOT_DEFINED if the list is empty
int first() const;
// return the last element of the list and LList::NOT_DEFINED if the list is empty
int last() const;
// delete the first occurrence of x in the list
// postcondition: return true if x was removed, false otherwise
// give the length of the list, i.e. the number of elements in this list
bool remove(int x);
// mutator method (function) that reverses the list
void reverse();
// mutator method to transfer all the elements from the 'other' list into
// this list and place them at position pos of this list
// e.g. when pos == 0, all the elements of the 'other' list will
// be placed before the (original) elements of this list
// if pos >= length(), (i.e. if pos is greater than the length of this list),
// then all the elements of the 'other' list are appended to this list
// precondition: pos >= 0, length() >= 0, other.length() >= 0
// postcondition: all the elements of the 'other' list are removed from the 'other' list
// and placed into this list starting at position pos
// The lengths of both lists change:
// other.length() becomes 0 (all the elements get removed)
// All the elements of the 'other' list are now in this list and the first element
// of the 'other' list is at position pos of this list or at the end if pos >= length()
void splice(LList& other, int pos);
// return true if the lfSide list is equal to the rtSide list
friend bool operator == (const LList& lfSide, const LList& rtSide);
// copy constructor
// we assuming that there is enough heap memory to make a copy
LList(const LList&);
// overloaded assignment operator
// we assuming that there is enough heap memory to make a copy
LList& operator = (const LList&);
// destructor
~LList();
// string that separates the values of the list in toString
static const std::string DELIMITER;
// value returned when there is no last or no first element in the list
static const int NOT_DEFINED;
#ifndef NDEBUG
// dump the array (print the contents of the internal array)
// using the ostream passed which defaults to std::cout
void dumpNodesArray(std::ostream& out = std::cout) const;
#endif
private:
struct Node {
int item;
int next;
};
// the array of nodes has INITIAL_CAPACITY nodes
static const int INITIAL_CAPACITY = 4;
// dynamic array of nodes
Node *nodes;
// index of the first node in the linked list
int head;
// index of the first node of the "free nodes' list"
int free;
};
My question is where should i declare this dynamic array. In LList()
this function? In the private?
Also how to declare this array so that its size can be increased when it is full.
Can someone help me to find a place to start.