A singly linked list C++ library
See test.cpp
for examples.
template <typename T>
partially specialized for T
or T *
.
LinkedList<int> list = LinkedList<int>();
LinkedList<Card *> list = LinkedList<Card *>();
LinkedList()
Create a new LinkedList
with no data.
LinkedList(LinkedList const &source)
Create a new LinkedList
with copy of data from source.
Parameters:
LinkedList const &source
: list to copy data from.
~LinkedList()
Calls clear()
.
void clear()
Clears and deletes all nodes and data.
void add(T data)
void add(T *data)
Adds a new node with specified data to the end of the linked list.
Parameters:
T data
or T *data
: primitive data or pointer to new data to be stored
int size()
Returns the number of nodes.
Returns:
int
: the size of the linked list.
void set(const int index, T data)
void set(const int index, T *data)
Replaces the data of the node at the specified index with new data.
Parameters:
const int index
: index of desired node
T data
or T *data
: primitive data or pointer to new data to be stored
void insert(const int index, T data)
void insert(const int index, T *data)
Inserts a new node and new data before the specified index.
Parameters:
const int index
: index of node to be after new node
T data
or T *data
: primitive data or pointer to new data to be stored
void remove(const int index)
Removes the node and data at the specified index.
Parameters:
const int index
: index of node to be deleted
bool empty()
Returns true if the linked list has no elements in it.
Returns:
bool
: whether the linked list is empty
void swap(const int index1, const int index2)
Switches the data of the nodes at the specified indexes.
Parameters:
const int index1
: index of the first specified node
const int index2
: index of the second specified node
LinkedList<T> * subList(const int start, const int length)
Creates and returns a new list containing data from a sub-range of the linked list.
Parameters:
const int start
: starting index of the sub-range
const int length
: length of the sub-range (start + length is not included in the new linked list)
Returns:
LinkedList *
: a pointer to the new linked list
T operator[](const int index)
T *operator[](const int index)
Access data at the given index.
Parameters:
const int index
: index of desired node
Returns:
T
or T *
: primitive data or pointer to data
std::ostream &operator<<(std::ostream &os, const LinkedList<T> &list)
std::ostream &operator<<(std::ostream &os, const LinkedList<T *> &list)
Print the data in the list to the ostream. The class in a pointer list T *
must also overload the <<
operator.
Parameters:
std::ostream &os
: ostream data being added to
const LinkedList<T> &list
or const LinkedList<T *> &list
: list to be printed
Returns: std::ostream &
: ostream with added data
GPL-3.0 License