This project implements a custom list container in C++ that uses a doubly-linked list as its underlying data structure. The implementation follows the interface of std::list while providing a robust foundation for list operations with comprehensive memory management.
The custom list is implemented through a template class with an underlying Node structure that supports bidirectional linking. Key features include:
- Template-based implementation supporting any data type
- Custom allocator support
- Doubly-linked structure with previous and next pointers
- Memory-efficient node management
- Support for both copy and move semantics
- STL-compatible iterator implementation
The main list class template with two parameters:
- T: Type of elements stored in the list
- A: Allocator type (defaults to std::allocator)
Internal node structure containing:
data: The stored elementpNext: Pointer to the next nodepPrev: Pointer to the previous node
Custom bidirectional iterator class supporting:
- Forward and backward traversal
- Standard iterator operations (++, --, *, ==, !=)
- STL container compatibility
The implementation includes standard list operations:
front(): Access first elementback(): Access last element
push_front(): Insert element at beginningpush_back(): Insert element at endpop_front(): Remove first elementpop_back(): Remove last elementinsert(): Insert element at positionerase(): Remove element at positionclear(): Remove all elementsswap(): Exchange contents with another list
empty(): Check if list is emptysize(): Get number of elements
- Default constructor
- Size constructor
- Range constructor
- Copy constructor
- Move constructor
- Initializer list constructor
- Assignment operators (copy, move, initializer list)
#include "list.h"
// Create a list of integers
custom::list<int> myList;
// Add elements
myList.push_back(1);
myList.push_front(0);
myList.push_back(2);
// Iterate through the list
for (auto it = myList.begin(); it != myList.end(); ++it)
std::cout << *it << " ";
// Use range-based for loop
for (const auto& item : myList)
std::cout << item << " ";- Efficient node allocation using custom allocator support
- Proper cleanup in destructors and clear operations
- Prevention of memory leaks in all operations
- Exception-safe operations
list.h: Main list implementation with Node and iterator classes- Unit tests (not included in repository)
The project includes Visual Studio solution files for building on Windows. Open LabList.sln and build using Visual Studio 2019 or later.
- This is an educational implementation demonstrating list container concepts
- The implementation closely follows the std::list interface
- Special attention is paid to memory management and exception safety
- Supports both forward and backward traversal through bidirectional iterators
This code is provided for educational purposes. See included license file for terms of use.
Nathan Bird
nathanbirdka@gmail.com