This repository contains a Python implementation of a singly linked list using classes.
A singly linked list is a data structure consisting of a sequence of nodes, where each node contains a value and a reference (link) to the next node in the sequence. The first node is called the head, and the last node is called the tail.
In this implementation, we have defined a Node
class that represents a node in the linked list, and a LinkedList
class that represents the linked list itself.
- Clone the repository
- Navigate to the project directory
Create a new linked list:
my_list = LinkedList()
Add a new node to the list:
my_list.add_node(5)
Traverse the list:
my_list.traverse()
Delete a node from the list:
my_list.delete_node(5)
Represents a node in the linked list.
data
: The value of the node.next_node
: A reference to the next node in the list.
__init__(self, data)
: Initializes a new instance of theNode
class.
Represents a singly linked list.
head
: The first node in the list.size
: The number of nodes in the list.
__init__(self)
: Initializes a new instance of theLinkedList
class.add_node(self, data)
: Adds a new node with the specified value to the end of the list.delete_node(self, data)
: Deletes the first node with the specified value from the list.traverse(self)
: Traverses the list and prints the value of each node.
This project provides a basic implementation of a singly linked list in Python using classes. You can customize and extend the implementation based on your specific requirements.