pax_global_header00006660000000000000000000000064145671066040014523gustar00rootroot0000000000000052 comment=f3c66fd50e6982d065f88982c7033053dad496ba doubly_py_linked_list-1.1.2/000077500000000000000000000000001456710660400161135ustar00rootroot00000000000000doubly_py_linked_list-1.1.2/.github/000077500000000000000000000000001456710660400174535ustar00rootroot00000000000000doubly_py_linked_list-1.1.2/.github/workflows/000077500000000000000000000000001456710660400215105ustar00rootroot00000000000000doubly_py_linked_list-1.1.2/.github/workflows/ci.yml000066400000000000000000000024201456710660400226240ustar00rootroot00000000000000# This workflow will install Python dependencies, run tests and lint with a variety of Python versions # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python name: CI on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: build: runs-on: ubuntu-latest strategy: fail-fast: false matrix: python-version: ["3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v3 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install --upgrade pip python -m pip install flake8 python -m pip install -e . - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | python -m unittest tests.test_doubly_linked_list doubly_py_linked_list-1.1.2/.gitignore000066400000000000000000000060061456710660400201050ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover *.py,cover .hypothesis/ .pytest_cache/ cover/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 db.sqlite3-journal # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder .pybuilder/ target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv # For a library or package, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: # .python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies # having no cross-platform support, pipenv may install dependencies that don't work, or not # install all needed dependencies. #Pipfile.lock # poetry # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. # This is especially recommended for binary packages to ensure reproducibility, and is more # commonly ignored for libraries. # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control #poetry.lock # pdm # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. #pdm.lock # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it # in version control. # https://pdm.fming.dev/#use-with-ide .pdm.toml # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm __pypackages__/ # Celery stuff celerybeat-schedule celerybeat.pid # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/ # pytype static type analyzer .pytype/ # Cython debug symbols cython_debug/ # PyCharm # JetBrains specific template is maintained in a separate JetBrains.gitignore that can # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ doubly_py_linked_list-1.1.2/.pre-commit-config.yaml000066400000000000000000000007171456710660400224010ustar00rootroot00000000000000repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace - id: check-case-conflict - id: check-json - id: check-yaml - id: check-toml - id: check-merge-conflict - repo: https://github.com/psf/black rev: 23.3.0 hooks: - id: black - repo: https://github.com/PyCQA/autoflake rev: v1.5.3 hooks: - id: autoflake doubly_py_linked_list-1.1.2/LICENSE000066400000000000000000000027661456710660400171330ustar00rootroot00000000000000BSD 3-Clause License Copyright (c) 2024, Konstantin (Konze) Lübeck All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. doubly_py_linked_list-1.1.2/README.md000066400000000000000000000016721456710660400174000ustar00rootroot00000000000000# Doubly (Py) Linked List 1️⃣ ↔️ 2️⃣ ↔️ 3️⃣ [![CI](https://github.com/k0nze/doubly_py_linked_list/actions/workflows/ci.yml/badge.svg)](https://github.com/k0nze/doubly_py_linked_list/actions/workflows/ci.yml) [![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) A module that implements doubly linked lists in python ## Example ``` python -m pip install doubly-py-linked-list ``` ```python >>> from doubly_py_linked_list import DoublyLinkedList as dll >>> d = dll([1, 2, 3, 4]) >>> d 1 <-> 2 <-> 3 <-> 4 >>> node_0 = d.insert_head(0) >>> node_5 = d.insert_tail(5) >>> for v in d: ... print(v) ... 0 1 2 3 4 5 >>> d.move_to_head(node_5) >>> d.move_to_tail(node_0) >>> list(d) [5, 1, 2, 3, 4, 0] >>> d.pop_head() 5 >>> list(d) [1, 2, 3, 4, 5, 0] >>> d.pop_tail() 0 >>> list(d) [1, 2, 3, 4] >>> d.nodes() [ddl_node(1), ddl_node(2), ddl_node(3), ddl_node(4)] ``` doubly_py_linked_list-1.1.2/pyproject.toml000066400000000000000000000020401456710660400210230ustar00rootroot00000000000000# pyproject.toml [build-system] requires = ["setuptools>=61.0.0", "wheel"] build-backend = "setuptools.build_meta" [project] name = "doubly_py_linked_list" version = "1.1.2" description = "Doubly linked list implementation in Python" readme = "README.md" authors = [{ name = "Konstantin (k0nze) Lübeck", email = "admin@konze.org" }] license = { file = "LICENSE" } classifiers = [ "License :: OSI Approved :: BSD License", "Programming Language :: Python", "Programming Language :: Python :: 3", ] keywords = ["data structures"] dependencies = [] requires-python = ">=3.8" [project.optional-dependencies] dev = ["pre-commit", "bumpver"] [project.urls] Homepage = "https://github.com/k0nze/doubly_py_linked_list" [tool.bumpver] current_version = "1.1.2" version_pattern = "MAJOR.MINOR.PATCH" commit_message = "bump version {old_version} -> {new_version}" commit = true tag = true push = true [tool.bumpver.file_patterns] "pyproject.toml" = ['version = "{version}"',] "src/doubly_py_linked_list/__init__.py" = ['__version__ = "{version}"'] doubly_py_linked_list-1.1.2/setup.py000066400000000000000000000000461456710660400176250ustar00rootroot00000000000000from setuptools import setup setup() doubly_py_linked_list-1.1.2/src/000077500000000000000000000000001456710660400167025ustar00rootroot00000000000000doubly_py_linked_list-1.1.2/src/doubly_py_linked_list/000077500000000000000000000000001456710660400232715ustar00rootroot00000000000000doubly_py_linked_list-1.1.2/src/doubly_py_linked_list/__init__.py000066400000000000000000000001751456710660400254050ustar00rootroot00000000000000__version__ = "1.1.2" from .doubly_linked_list import DoublyLinkedListNode from .doubly_linked_list import DoublyLinkedList doubly_py_linked_list-1.1.2/src/doubly_py_linked_list/doubly_linked_list.py000066400000000000000000000165111456710660400275260ustar00rootroot00000000000000from typing import List, Any, Optional class DoublyLinkedListNode: """ A node in a doubly linked list Attributes ---------- value: Any The value of the node next: Optional[DoublyLinkedListNode] The next node in the doubly linked list prev: Optional[DoublyLinkedListNode] The previous node in the doubly linked list """ def __init__(self, value: Any): """ Create a new node in a doubly linked list """ self.value = value self.next: Optional[DoublyLinkedListNode] = None self.prev: Optional[DoublyLinkedListNode] = None def __repr__(self) -> str: return f"ddl_node({self.value})" def __str__(self) -> str: return self.__repr__() class DoublyLinkedList: """ A doubly linked list Attributes ---------- head: Optional[DoublyLinkedListNode] The head of the doubly linked list tail: Optional[DoublyLinkedListNode] The tail of the doubly linked list length: int The number of nodes in the doubly linked list """ def __init__(self, values: Optional[List[Any]] = None): """ Create a new doubly linked list Parameters ---------- values: List[Any] A list of values to initialize the doubly linked list with """ self.head: Optional[DoublyLinkedListNode] = None self.tail: Optional[DoublyLinkedListNode] = None self.length: int = 0 if values is not None: for value in values: self.insert_tail(value) def insert_head(self, value: Any) -> DoublyLinkedListNode: """ Insert a value at the head of the doubly linked list Parameters ---------- value: Any The value to insert Returns: -------- DoublyLinkedListNode The node that was inserted at the head of the doubly linked list """ new_node = DoublyLinkedListNode(value) if self.head is None: self.head = new_node self.tail = new_node else: new_node.next = self.head self.head.prev = new_node self.head = new_node self.length += 1 return new_node def insert_tail(self, value) -> DoublyLinkedListNode: """ Insert a value at the tail of the doubly linked list Parameters ---------- value: Any The value to insert Returns ------- DoublyLinkedListNode The node that was inserted at the tail of the doubly linked list """ new_node = DoublyLinkedListNode(value) if self.tail is None: self.head = new_node self.tail = new_node else: new_node.prev = self.tail self.tail.next = new_node self.tail = new_node self.length += 1 return new_node def get_head(self) -> Any: """ Get the value of the head of the doubly linked list Returns ------- Any The value of the head of the doubly linked list """ if self.head is None: return None else: return self.head.value def get_tail(self) -> Any: """ Get the value of the tail of the doubly linked list Returns ------- Any The value of the tail of the doubly linked list """ if self.tail is None: return None else: return self.tail.value def remove(self, node: DoublyLinkedListNode) -> None: """ Remove a node from the doubly linked list Parameters ---------- node: DoublyLinkedListNode The node to remove """ if node is self.head and node is self.tail: self.head = None self.tail = None elif node is self.head: self.head = node.next node.next.prev = None elif node is self.tail: self.tail = node.prev node.prev.next = None else: next_node = node.next prev_node = node.prev next_node.prev = prev_node prev_node.next = next_node self.length -= 1 node.prev = None node.next = None def pop_head(self) -> Any: """ Remove the head of the doubly linked list and return its value Returns ------- Any The value of the head of the doubly linked list """ if self.head is None: return None else: return_value = self.head.value self.remove(self.head) return return_value def pop_tail(self) -> Any: """ Remove the tail of the doubly linked list and return its value Returns ------- Any The value of the tail of the doubly linked list """ if self.tail is None: return None else: return_value = self.tail.value self.remove(self.tail) return return_value def move_to_head(self, node: DoublyLinkedListNode) -> None: """ Move a node to the head of the doubly linked list Parameters ---------- node: DoublyLinkedListNode The node to move to the head of the doubly linked list """ if node is self.head: return else: self.remove(node) node.next = self.head self.head.prev = node self.head = node def move_to_tail(self, node: DoublyLinkedListNode) -> None: """ Move a node to the tail of the doubly linked list Parameters ---------- node: DoublyLinkedListNode The node to move to the tail of the doubly linked list """ if node is self.tail: return else: self.remove(node) node.prev = self.tail self.tail.next = node self.tail = node def nodes(self) -> List[DoublyLinkedListNode]: """ Return a list of the nodes in the doubly linked list Returns ------- List[DoublyLinkedListNode] A list of the nodes in the doubly linked list """ nodes = [] current_node = self.head while current_node is not None: nodes.append(current_node) current_node = current_node.next return nodes def __len__(self) -> int: return self.length def __iter__(self) -> "DoublyLinkedList": self.current_iter_node = self.head return self def __next__(self) -> DoublyLinkedListNode: if self.current_iter_node is not None: return_node = self.current_iter_node self.current_iter_node = self.current_iter_node.next return return_node.value raise StopIteration def __repr__(self) -> str: node_values = [] if self.head is None: return "empty DoublyLinkedList" current_node = self.head node_values.append(str(current_node)) while current_node.next != None: current_node = current_node.next node_values.append(str(current_node)) return " <-> ".join(node_values) doubly_py_linked_list-1.1.2/tests/000077500000000000000000000000001456710660400172555ustar00rootroot00000000000000doubly_py_linked_list-1.1.2/tests/test_doubly_linked_list.py000066400000000000000000000146021456710660400245500ustar00rootroot00000000000000import unittest from doubly_py_linked_list import DoublyLinkedList as dll class TestDoublyLinkedList(unittest.TestCase): def test_insert_head(self): d = dll() self.assertEqual(len(d), 0) d.insert_head(1) self.assertEqual(len(d), 1) self.assertEqual(d.head.value, 1) self.assertEqual(d.tail.value, 1) d.insert_head(2) self.assertEqual(len(d), 2) self.assertEqual(d.head.value, 2) self.assertEqual(d.tail.value, 1) d.insert_head(3) self.assertEqual(len(d), 3) self.assertEqual(d.head.value, 3) self.assertEqual(d.tail.value, 1) def test_add_behind_tail(self): d = dll() self.assertEqual(len(d), 0) d.insert_tail(1) self.assertEqual(len(d), 1) self.assertEqual(d.head.value, 1) self.assertEqual(d.tail.value, 1) d.insert_tail(2) self.assertEqual(len(d), 2) self.assertEqual(d.head.value, 1) self.assertEqual(d.tail.value, 2) d.insert_tail(3) self.assertEqual(len(d), 3) self.assertEqual(d.head.value, 1) self.assertEqual(d.tail.value, 3) def test_remove_node(self): d = dll() self.assertEqual(len(d), 0) node_1 = d.insert_tail(1) node_2 = d.insert_tail(2) node_3 = d.insert_tail(3) d.insert_tail(4) d.insert_tail(5) node_6 = d.insert_tail(6) node_7 = d.insert_tail(7) self.assertEqual(len(d), 7) l = list(d) self.assertListEqual(l, [1, 2, 3, 4, 5, 6, 7]) d.remove(node_3) self.assertEqual(len(d), 6) l = list(d) self.assertListEqual(l, [1, 2, 4, 5, 6, 7]) d.remove(node_1) self.assertEqual(len(d), 5) self.assertEqual(d.head, node_2) l = list(d) self.assertListEqual(l, [2, 4, 5, 6, 7]) d.remove(node_7) self.assertEqual(len(d), 4) self.assertEqual(d.tail, node_6) l = list(d) self.assertListEqual(l, [2, 4, 5, 6]) def test_move_to_head(self): d = dll() d.insert_tail(1) d.insert_tail(2) node_3 = d.insert_tail(3) d.insert_tail(4) node_5 = d.insert_tail(5) node_6 = d.insert_tail(6) self.assertEqual(len(d), 6) d.move_to_head(node_3) self.assertEqual(d.head.value, 3) self.assertEqual(d.head, node_3) l = list(d) self.assertListEqual(l, [3, 1, 2, 4, 5, 6]) d.move_to_head(node_6) self.assertEqual(d.head.value, 6) self.assertEqual(d.head, node_6) self.assertEqual(d.tail.value, 5) self.assertEqual(d.tail, node_5) l = list(d) self.assertListEqual(l, [6, 3, 1, 2, 4, 5]) d.move_to_head(node_6) self.assertEqual(d.head.value, 6) self.assertEqual(d.head, node_6) l = list(d) self.assertListEqual(l, [6, 3, 1, 2, 4, 5]) def test_move_to_tail(self): d = dll() node_1 = d.insert_tail(1) node_2 = d.insert_tail(2) node_3 = d.insert_tail(3) d.insert_tail(4) d.insert_tail(5) d.insert_tail(6) self.assertEqual(len(d), 6) d.move_to_tail(node_3) self.assertEqual(d.tail.value, 3) self.assertEqual(d.tail, node_3) l = list(d) self.assertListEqual(l, [1, 2, 4, 5, 6, 3]) d.move_to_tail(node_1) self.assertEqual(d.tail.value, 1) self.assertEqual(d.tail, node_1) self.assertEqual(d.head.value, 2) self.assertEqual(d.head, node_2) l = list(d) self.assertListEqual(l, [2, 4, 5, 6, 3, 1]) d.move_to_tail(node_1) self.assertEqual(d.tail.value, 1) self.assertEqual(d.tail, node_1) l = list(d) self.assertListEqual(l, [2, 4, 5, 6, 3, 1]) def test_init_with_values(self): d = dll([1, 2, 3, 4, 5, 6, 7]) self.assertEqual(len(d), 7) l = list(d) self.assertListEqual(l, [1, 2, 3, 4, 5, 6, 7]) def test_nodes(self): d = dll() node_1 = d.insert_tail(1) node_2 = d.insert_tail(2) node_3 = d.insert_tail(3) node_4 = d.insert_tail(4) nodes = d.nodes() self.assertListEqual(nodes, [node_1, node_2, node_3, node_4]) def test_pop_head(self): d = dll([1, 2, 3, 4, 5, 6, 7]) self.assertEqual(len(d), 7) self.assertEqual(d.pop_head(), 1) self.assertEqual(d.get_head(), 2) self.assertEqual(len(d), 6) self.assertEqual(d.pop_head(), 2) self.assertEqual(d.get_head(), 3) self.assertEqual(len(d), 5) self.assertEqual(d.pop_head(), 3) self.assertEqual(d.get_head(), 4) self.assertEqual(len(d), 4) self.assertEqual(d.pop_head(), 4) self.assertEqual(d.get_head(), 5) self.assertEqual(len(d), 3) self.assertEqual(d.pop_head(), 5) self.assertEqual(d.get_head(), 6) self.assertEqual(len(d), 2) self.assertEqual(d.pop_head(), 6) self.assertEqual(d.get_head(), 7) self.assertEqual(len(d), 1) self.assertEqual(d.pop_head(), 7) self.assertEqual(d.get_head(), None) self.assertEqual(len(d), 0) self.assertEqual(d.head, None) self.assertEqual(d.tail, None) self.assertEqual(d.pop_head(), None) def test_pop_tail(self): d = dll([1, 2, 3, 4, 5, 6, 7]) self.assertEqual(len(d), 7) self.assertEqual(d.pop_tail(), 7) self.assertEqual(d.get_tail(), 6) self.assertEqual(len(d), 6) self.assertEqual(d.pop_tail(), 6) self.assertEqual(d.get_tail(), 5) self.assertEqual(len(d), 5) self.assertEqual(d.pop_tail(), 5) self.assertEqual(d.get_tail(), 4) self.assertEqual(len(d), 4) self.assertEqual(d.pop_tail(), 4) self.assertEqual(d.get_tail(), 3) self.assertEqual(len(d), 3) self.assertEqual(d.pop_tail(), 3) self.assertEqual(d.get_tail(), 2) self.assertEqual(len(d), 2) self.assertEqual(d.pop_tail(), 2) self.assertEqual(d.get_tail(), 1) self.assertEqual(len(d), 1) self.assertEqual(d.pop_tail(), 1) self.assertEqual(d.get_tail(), None) self.assertEqual(len(d), 0) self.assertEqual(d.head, None) self.assertEqual(d.tail, None) self.assertEqual(d.pop_tail(), None)