https://github.com/tyhil/linked-list
A singly linked list C++ library
https://github.com/tyhil/linked-list
cpp linked-list pointer
Last synced: 8 months ago
JSON representation
A singly linked list C++ library
- Host: GitHub
- URL: https://github.com/tyhil/linked-list
- Owner: TyHil
- License: gpl-3.0
- Created: 2021-01-13T22:19:11.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2023-05-04T17:16:33.000Z (over 2 years ago)
- Last Synced: 2025-02-16T18:40:06.402Z (10 months ago)
- Topics: cpp, linked-list, pointer
- Language: C++
- Homepage:
- Size: 47.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# linked-list
[](https://repl.it/github/TyHil/linked-list)
## Description
A singly linked list C++ library
## Documentation
See `test.cpp` for examples.
`template ` partially specialized for `T` or `T *`.
### Initialization examples
`LinkedList list = LinkedList();`
`LinkedList list = LinkedList();`
### Constructor
`LinkedList()`
Create a new `LinkedList` with no data.
### Copy Constructor
`LinkedList(LinkedList const &source)`
Create a new `LinkedList` with copy of data from source.
Parameters:
`LinkedList const &source`: list to copy data from.
### Destructor
`~LinkedList()`
Calls `clear()`.
### Clear
`void clear()`
Clears and deletes all nodes and data.
### Add
`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
### Size
`int size()`
Returns the number of nodes.
Returns:
`int`: the size of the linked list.
### Set
`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
### Insert
`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
### Remove
`void remove(const int index)`
Removes the node and data at the specified index.
Parameters:
`const int index`: index of node to be deleted
### Empty
`bool empty()`
Returns true if the linked list has no elements in it.
Returns:
`bool`: whether the linked list is empty
### Swap
`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
### Sub List
`LinkedList * 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
### Overload `[]`
`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
### Overload `<<`
`std::ostream &operator<<(std::ostream &os, const LinkedList &list)`
`std::ostream &operator<<(std::ostream &os, const LinkedList &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 &list` or `const LinkedList &list`: list to be printed
Returns: `std::ostream &`: ostream with added data
## License
GPL-3.0 License