Singly linked list in Python
Singly linked list in Python
I'm happy to provide a response in English! Here's a detailed explanation of singly linked lists in Python:
What is a Linked List?
A linked list is a linear collection of nodes, each containing a reference (i.e., a "link") to the next node in the sequence. This allows for efficient insertion and deletion of nodes at any position in the list.
Singly Linked List Definition:
A singly linked list, also known as a one-way linked list, is a type of linked list where each node only has a reference (or pointer) to the next node in the sequence, but not to the previous node. This makes it simpler and more efficient compared to doubly linked lists.
Node Structure:
Each node in a singly linked list typically consists of three components:
Data: The actual value stored by the node. Next: A reference (or pointer) to the next node in the sequence. Previous (optional): In a doubly linked list, this would be the reference to the previous node.In our implementation, we'll focus on the simplest case with only two components: Data and Next.
Implementing Singly Linked List in Python:
Here's an example implementation of a singly linked list class in Python:
class Node:
def init(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def init(self):
self.head = None
def append(self, data):
if not self.head:
self.head = Node(data)
else:
current = self.head
while current.next:
current = current.next
current.next = Node(data)
def print_list(self):
current = self.head
while current:
print(current.data)
current = current.next
Example usage:sll = SinglyLinkedList()
sll.append(1)
sll.append(2)
sll.append(3)
sll.print_list() # Output: 1, 2, 3
In this implementation:
TheNode
class represents individual nodes in the linked list, with a Data attribute and a Next attribute. The SinglyLinkedList
class encapsulates the linked list itself. It has a Head attribute pointing to the first node in the list (or None
if the list is empty). The append
method adds new nodes to the end of the linked list, efficiently inserting them by traversing the existing list and updating the last node's Next
reference. The print_list
method iterates through the linked list, printing each node's data.
Advantages:
Efficient insertion and deletion: Since we only store references to the next node in the sequence, inserting or deleting a node at any position is relatively straightforward (compared to doubly linked lists). Space efficiency: Singly linked lists typically use less memory than doubly linked lists since each node only has one reference.Disadvantages:
Only forward traversal possible: Since we don't store references to the previous node, traversing the list in reverse order is not as straightforward. More complex insertion and deletion at arbitrary positions: While still relatively efficient, inserting or deleting a node at any position requires traversing the list from the beginning to find the desired node.I hope this helps! Let me know if you have any further questions.
How to take input in linked list in Python?
Here's an explanation of how to create a linked list and add elements to it in Python:
In Python, you can create a linked list by defining a Node class that has two attributes: data
(the value stored in the node) and next
(a reference to the next node in the list). The main difference between a linked list and an array is how data is stored.
Here's a simple implementation of a Node and a LinkedList:
class Node:
def init(self, data=None):
self.data = data
self.next = None
class LinkedList:
def init(self):
self.head = None
To create a new node with some value, you can do something like this:
def append(self, data):
if not self.head:
self.head = Node(data)
else:
current_node = self.head
while current_node.next:
current_node = current_node.next
current_node.next = Node(data)
Creating a new linked list and adding some elements to it
my_list = LinkedList()
my_list.append('A')
my_list.append('B')
my_list.append('C')
To take input from the user for a linked list, you can use a while
loop:
def main():
my_list = LinkedList()
while True:
user_input = input("Enter some data (or 'stop' to stop adding): ")
if user_input.lower() == 'stop':
break
else:
my_list.append(user_input)
main()
In this code, my_list
is the linked list where we're going to add elements. The program then asks the user for some data and adds it to the linked list until the user decides to stop adding.
Here's the full code:
class Node:
def init(self, data=None):
self.data = data
self.next = None
class LinkedList:
def init(self):
self.head = None
def append(self, data):
if not self.head:
self.head = Node(data)
else:
current_node = self.head
while current_node.next:
current_node = current_node.next
current_node.next = Node(data)
def main():
my_list = LinkedList()
while True:
user_input = input("Enter some data (or 'stop' to stop adding): ")
if user_input.lower() == 'stop':
break
else:
my_list.append(user_input)
current_node = my_list.head
print("Your linked list is:")
while current_node:
print(current_node.data)
current_node = current_node.next
main()
This program creates a new node for each piece of data the user inputs, and then adds it to the end of the linked list.