From 4a8fa8bfebe2a623a5a24169458f851634df0221 Mon Sep 17 00:00:00 2001 From: James Mc Dermott Date: Fri, 14 Oct 2016 17:23:07 +0100 Subject: [PATCH] Create __init__.py Initialising a LinkedList class, using a Node class to store the item and the next pointer. --- data_structures/LinkedList/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 data_structures/LinkedList/__init__.py diff --git a/data_structures/LinkedList/__init__.py b/data_structures/LinkedList/__init__.py new file mode 100644 index 000000000..1d220599f --- /dev/null +++ b/data_structures/LinkedList/__init__.py @@ -0,0 +1,22 @@ +class Node: + def __init__(self, item, next): + self.item = item + self.next = next + +class LinkedList: + def __init__(self): + self.head = None + + def add(self, item): + self.head = Node(item, self.head) + + def remove(self): + if self.is_empty(): + return None + else: + item = self.head.item + self.head = self.head.next + return item + + def is_empty(self): + return self.head == None