Skip to main content

A Python implementation of a singly linked list

Project description

Singly Linked List

This is a Python implementation of a singly linked list.

Usage

Singly Linked List

This is a Python implementation of a singly linked list.

Installation

Install the SingleLinkedList package from PyPI using pip:

pip install single-linked-list==0.0.1

then, import the SingleLinkedList class

from slll.slll import SingleLinkedList

Initialize a Singly Linked List

single_linked_list = SingleLinkedList()

Methods

* Append

  • inserts a new node at the end of the linked list.

Method:

  • append(data)

data(any) : The data to be stored in the new node.

single_linked_list.append(1)
single_linked_list.append(2)

* Preappend

  • inserts a new node at the beginning of the linked list.

Method:

  • preappend(data)

data(any) : The data to be stored in the new node.

single_linked_list.preappend(0)

* Insert

  • inserts a new node at the specified index in the linked list.

Method:

  • insert(index, data)

index(int) : The index where the new node should be inserted. data(any) : The data to be stored in the new node.

single_linked_list.insert(2, 3)

* Find

  • returns the index of the first node containing the specified data.

Method:

  • find(data)

data(any): The data to search for.

index = single_linked_list.find(2)
print(index)  # Output: 1

* Get

  • returns the node at the specified index.

Method:

  • get(index)

index(int) : The index of the desired node.

node = single_linked_list.get(2)
print(node.data)  # Output: 3

* Set

  • updates the data of the node at the specified index.

Method:

  • set(index, data)

index(int) : The index of the node to be updated. data(any) : The new data to be stored in the node.

single_linked_list.set(0, 9000)

* Pop

  • removes and returns the last node in the linked list.

Method:

  • pop()
last_node = single_linked_list.pop()
print(last_node)  # Output: 3

* Pop First

  • removes and returns the first node in the linked list.

Method:

  • pop_first()
first_node = single_linked_list.pop_first()
print(first_node)  # Output: 0

* Remove

  • removes the node at the specified index from the linked list.

Method:

  • remove(index)

index(int) : The index of the node to be removed.

single_linked_list.remove(2)

* Clear

  • clears all nodes in the linked list.

Method:

  • clear()
single_linked_list.clear()

* Get Length

  • returns the current size (length) of the linked list.

Method:

  • len()
length = len(single_linked_list)
print(length)  # Output: 1

* Traverse and print

  • traverses the linked list and prints the data of each node.

Method:

  • traverse()
single_linked_list.traverse()
# Output: 0 -> 1 -> 3

* String representation

  • returns a string representation of the linked list.

Method:

  • str()
linked_list_str

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

single-linked-list-0.0.2.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

single_linked_list-0.0.2-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file single-linked-list-0.0.2.tar.gz.

File metadata

  • Download URL: single-linked-list-0.0.2.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for single-linked-list-0.0.2.tar.gz
Algorithm Hash digest
SHA256 95ca3a14636df8ad7cb63c0de126e2ce7a4ee6a8091172b5ae6195e7dc7862e9
MD5 9d052be6e8c6df1d0c4490525cf10c8c
BLAKE2b-256 55aa34b6acfdd1272b1f8be8ee234b952e7ab394667cc50fb3f6fbcd3ce45149

See more details on using hashes here.

File details

Details for the file single_linked_list-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for single_linked_list-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 394f10a61c7b7286353d31f42d6decfec02900fb15e0b07b4ca0463b55c89178
MD5 81a7246934efb33e3681b9479286f540
BLAKE2b-256 73bed7a2a958463ae6ee729f13c9a985dbb42a43c57ba4c19e053a9e8b375ef0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page