mp_lists
Loathsome Lists
|
List: This is a templated linked list class (meaning it contains data of templated type T, which is a placeholder for a type). More...
#include <List.h>
Classes | |
class | ListNode |
The ListNode class is private to the List class via the principle of encapsulation—the end user does not need to know our node-based implementation details. More... | |
Public Member Functions | |
List () | |
Default List constructor. More... | |
List (const List< T > &other) | |
Copy constructor for a List. More... | |
List< T > & | operator= (const List< T > &rhs) |
Overloaded assignment operator for Lists. More... | |
int | size () const |
Gets the size of the List. More... | |
void | print (ostream &os=std::cout) const |
Used to print the list. More... | |
bool | empty () const |
Determines if the current List is empty. More... | |
~List () | |
Destroys the current List. More... | |
void | insertFront (const T &ndata) |
Inserts a new node at the front of the List. More... | |
void | insertBack (const T &ndata) |
Inserts a new node at the back of the List. More... | |
void | reverse () |
Reverses the current List. More... | |
void | reverseNth (int n) |
Reverses blocks of size n in the current List. More... | |
void | waterfall () |
Modifies the List using the waterfall algorithm. More... | |
List< T > | split (int splitPoint) |
Splits the given list into two parts by dividing it at the splitPoint. More... | |
void | mergeWith (List< T > &otherList) |
Merges the given sorted list into the current sorted list. More... | |
void | sort () |
Sorts the current list by applying the Mergesort algorithm. More... | |
ListIterator | begin () const |
Returns a ListIterator with a position at the beginning of the List. More... | |
ListIterator | end () const |
Returns a ListIterator one past the end of the List. More... | |
template<class Iter > | |
List (const Iter &start, const Iter &end) | |
Private Member Functions | |
void | _copy (const List< T > &other) |
Copies the given list into the current list. More... | |
void | _destroy () |
Destroys all dynamically allocated memory associated with the current List class. More... | |
void | reverse (ListNode *&startPoint, ListNode *&endPoint) |
Helper function to reverse a sequence of linked memory inside a List, starting at startPoint and ending at endPoint. More... | |
ListNode * | split (ListNode *start, int splitPoint) |
Helper function to split a sequence of linked memory at the node splitPoint steps after start. More... | |
ListNode * | merge (ListNode *first, ListNode *second) |
Helper function to merge two sorted and independent sequences of linked memory. More... | |
ListNode * | mergesort (ListNode *start, int chainLength) |
Sorts a chain of linked memory given a start node and a size. More... | |
Private Attributes | |
ListNode * | head_ |
The head of the List. More... | |
ListNode * | tail_ |
The tail of the list. More... | |
int | length_ |
The length of the current List. More... | |
List: This is a templated linked list class (meaning it contains data of templated type T, which is a placeholder for a type).
You should not remove anything from this class definition, but you will find it helpful to add your own helper functions to this class, and are encouraged to add to it.
Copy constructor for a List.
Since Lists allocate dynamic memory (i.e., they use "new", we must define the Big Three).
other | The list we are copying. |
Destroys the current List.
This function should ensure that memory does not leak on destruction of a list.
Copies the given list into the current list.
other | The List to be copied. |
|
private |
List< T >::ListIterator List< T >::begin |
Returns a ListIterator with a position at the beginning of the List.
bool List< T >::empty |
List< T >::ListIterator List< T >::end |
Returns a ListIterator one past the end of the List.
void List< T >::insertBack | ( | const T & | ndata | ) |
void List< T >::insertFront | ( | const T & | ndata | ) |
Helper function to merge two sorted and independent sequences of linked memory.
The result should be a single sequence that is itself sorted.
This function SHOULD NOT create ANY new List objects.
first | The starting node of the first sequence. |
second | The starting node of the second sequence. |
Sorts a chain of linked memory given a start node and a size.
This is the recursive helper for the Mergesort algorithm (i.e., this is the divide-and-conquer step).
start | Starting point of the chain. |
chainLength | Size of the chain to be sorted. |
Merges the given sorted list into the current sorted list.
otherList | List to be merged into the current list. |
Overloaded assignment operator for Lists.
Part of the Big Three that we must define because the class allocates dynamic memory.
rhs | The right hand side of the assignment statement. |
Used to print the list.
Const because it promises not to modify the current List.
os | Output stream to print the list to (e.g. cout) |
Helper function to reverse a sequence of linked memory inside a List, starting at startPoint and ending at endPoint.
You are responsible for updating startPoint and endPoint to point to the new starting and ending points of the rearranged sequence of linked memory in question.
startPoint | A pointer reference to the first node in the sequence to be reversed. |
endPoint | A pointer reference to the last node in the sequence to be reversed. |
void List< T >::reverseNth | ( | int | n | ) |
Reverses blocks of size n in the current List.
You should use your reverse( ListNode * &, ListNode * & ) helper function in this method!
n | The size of the blocks in the List to be reversed. |
int List< T >::size |
void List< T >::sort |
Sorts the current list by applying the Mergesort algorithm.
Splits the given list into two parts by dividing it at the splitPoint.
splitPoint | Point at which the list should be split into two. |
|
private |
Helper function to split a sequence of linked memory at the node splitPoint steps after start.
In other words, it should disconnect the sequence of linked memory after the given number of nodes, and return a pointer to the starting node of the new sequence of linked memory.
This function SHOULD NOT create ANY new List objects!
start | The node to start from. |
splitPoint | The number of steps to walk before splitting. |
In other words, it should disconnect the sequence of linked memory after the given number of nodes, and return a pointer to the starting node of the new sequence of linked memory.
This function SHOULD NOT create ANY new List or ListNode objects!
This function is also called by the public split() function located in List-given.hpp
start | The node to start from. |
splitPoint | The number of steps to walk before splitting. |
Modifies the List using the waterfall algorithm. Every other node (starting from the second one) is removed from the List, but appended at the back, becoming the new tail. This continues until the next thing to be removed is either the tail (not necessarily the original tail!) or NULL. You may NOT allocate new ListNodes. Note that since the tail should be continuously updated, some nodes will be moved more than once.
Reverses the current List.
Helper function to reverse a sequence of linked memory inside a List, starting at startPoint and ending at endPoint. You are responsible for updating startPoint and endPoint to point to the new starting and ending points of the rearranged sequence of linked memory in question.
startPoint | A pointer reference to the first node in the sequence to be reversed. |
endPoint | A pointer reference to the last node in the sequence to be reversed. |
Reverses blocks of size n in the current List. You should use your reverse( ListNode * &, ListNode * & ) helper function in this method!
n | The size of the blocks in the List to be reversed. |
Merges the given sorted list into the current sorted list.
otherList | List to be merged into the current list. |
Helper function to merge two sorted and independent sequences of linked memory. The result should be a single sequence that is itself sorted.
This function SHOULD NOT create ANY new List objects.
first | The starting node of the first sequence. |
second | The starting node of the second sequence. |
Sorts a chain of linked memory given a start node and a size. This is the recursive helper for the Mergesort algorithm (i.e., this is the divide-and-conquer step).
Called by the public sort function in List-given.hpp
start | Starting point of the chain. |
chainLength | Size of the chain to be sorted. |
void List< T >::waterfall | ( | ) |
Modifies the List using the waterfall algorithm.
Every other node (starting from the second one) is removed from the List, but appended at the back, becoming the new tail. This continues until the next thing to be removed is either the tail (not necessarily the original tail!) or NULL. You may NOT allocate new ListNodes. Note that since the tail should be continuously updated, some nodes will be moved more than once.
|
private |
The length of the current List.
Do not forget to update it!
The tail of the list.
May be NULL if the List is empty.