LinkedList is a standard template-based implementation of a doubly-linked list. It provides efficient insertion and
deletion at both ends and supports bi-directional iteration.
LinkedList is suitable for collections where frequent insertions and removals are expected, especially at the
beginning or end of the sequence.
linkedlist_value_container_t) allocated on
the heap.iterator, const_iterator, reverse_iterator, and const_reverse_iterator
for compatibility with C++ standard library algorithms.#include "ds/LinkedList.h"
#include <iostream>
void listExample() {
LinkedList<int> list;
// Adding elements
list.add(10);
list.add(20);
list.insertAtBeginning(5);
// Iterating with standard loops
for (int value : list) {
std::cout << value << " ";
}
std::cout << std::endl;
// Removing elements
list.remove(1); // Removes element at index 1
}
LinkedList<T>()
LinkedList<T>(const T *src, int size)
size elements from the provided array src.~LinkedList()
void add(T item)
item to the end of the list.void insertAtBeginning(T item)
item to the beginning of the list.void addMany(const T *items, int count)
count elements from the items array to the end of the list.T pop()
T remove(int i)
i. This is an O(N) operation.bool removeByElement(T element)
element and removes it. Returns true if found and removed.void clear()
int size() const
T &get(int i) const
i. O(N) complexity.void resetCursor()
void cursorToLast()
bool isCursorValid()
true if the cursor currently points to a valid node.T &next()
T &getCursor()
void setCursor(int i)
i.The class provides standard begin(), end(), cbegin(), cend(), rbegin(), and rend() methods returning
appropriate iterator types.
LinkedList manages a sequence of nodes, where each node is an instance of linkedlist_value_container_t.
While LinkedList supports index-based access via get(int i) and remove(int i), these operations are O(N) as they
require traversing the list from either the head or the tail (depending on which is closer) to reach the target index.
For heavy index-based workloads, ArrayList would be a more appropriate choice.