Develop my own container — a singly linked list.
Read in other languages: English, Русский
A learning project that allowed me to better understand the structure of C++ containers and use them effectively.
To build this project on linux you need:
- If you don't have Cmake installed, install Cmake
- If the "Debug" or "Release" folders are not created:
mkdir Debug
mkdir Release
- Run the command for Debug and/or Release conf:
cmake -E chdir Debug/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Debug
cmake -E chdir Release/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Release
- Go to "Debug" or "Release" folder and build:
cmake --build .
- To Run program - in the debug or release folder run:
./single_linked_list
- C++17(STL)
- GCC (MinG w64) 11.2.0
- forward_list
- Template classes
- Iterators begin, end, before_begin
- iterator_category, value_type, difference_type, pointer, reference
- swap
- Three levels of exception security
- Installation and configuration of all required components in the development environment to run the application
- The use case and tests are shown in main.cpp .
is one of the basic linked data structures. To understand how it works is to take the first step towards developing more complex structures. In the standard library, a singly linked list is represented by a template of the forward_list
class.
The list item is called a "node". The list item can be represented as a Node structure, which contains the value of the item and a pointer to the next node.
- Default constructor that creates an empty list;
- The
GetSize
method, which returns the number of items in the list; - The
IsEmpty
method, which returns true if the list is empty, and false otherwise. PushFront
- inserts an element at the beginning of a singly linked list, and the 'Clearoperation clears the list.<br> The
PushFront` method provides a strict exception safety guarantee: if an exception is thrown during the operation of the method, the state of the list should be the same as before the method was called.- The
Clear
method clears the list and does not throw exceptions. - When the list is destroyed, all its elements are deleted.
- The
PopFront
method. Deletes the first element of a non-empty list in time O(1). Does not throw exceptions. - The
InsertAfter
method. During the time O(1) inserts a new value into the list following the element referenced by the iterator passed to insertAfter. The method provides a strict guarantee of exception safety. - The
EraseAfter
method. During O(1) removes from the list the element following the element referenced by the iterator passed toinsertAfter
. Does not throw exceptions. - Methods
before_begin
andcbefore_begin
. Returns iterators referring to a dummy position before the first element of the list. Such an iterator is used as a parameter for theInsertAfter
andEraseAfter
methods when it is necessary to insert or delete an element at the beginning of the list. This iterator cannot be dereferenced.
The Basic Iterator
class, on the basis of which the constant and non-constant iterator of the list are declared.
The list class implements a constant and non-constant version of the begin
and end
methods, which return iterators to the first element of the container and the position following the last element.
To make it convenient to get constant iterators, the cbegin
and cend
methods are implemented.
- Comparison operations ==, !=, <, >, <=, >=;< br>
- Exchange of the contents of two lists using the swap method and the template swap function;
- Construction of a singly linked list based on
initializer_list
. The sequence of elements of the created list andinitializer_list
is the same; - Reliable copy constructor and assignment operation. The assignment operation provides a strict guarantee of exception safety. If an exception is thrown during the assignment process, the contents of the left argument of the assignment operation will remain unchanged.