I'm new to Python and thus learning it by implementing a set of commonly used data structures. Here's my implementation of a LinkedList. First the Node class.
class Node:
def __init__(self, value = None, next = None):
self.value = value;
self.next = next;
Here's the LinkedList,
from node import Node
class LinkedList:
def __init__(self):
self.head = None;
self.size = 0;
def insert(self, item):
curr = Node();
curr.value = item
self.size += 1
if(self.head == None):
self.head = curr
else:
curr.next = self.head
self.head = curr
def remove(self, item):
if(self.head == None):
raise Exception('Removing from an empty list')
curr = self.head
if(curr.value == item):
self.head = curr.next
return
while(curr.next.value != item):
curr = curr.next
if(curr.next == None):
raise Exception('Item not present in list')
else:
curr.next = curr.next.next
def print_list(self):
curr = self.head;
while(curr):
print(curr.value, end=' ')
curr = curr.next
And finally the calling client,
from linked_list import LinkedList
list = LinkedList()
list.insert(2)
list.insert(5)
list.insert(7)
list.insert(12)
list.insert(13)
list.remove(13)
list.print_list()
My primary language is Java, so I'd invite comments on how pythonic is my implementation and best practices. TIA.