Last active
February 9, 2016 20:03
-
-
Save choncou/0caeaa45c4c5183e0a24 to your computer and use it in GitHub Desktop.
Implementation of Linked List data structure with broad set of functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList: | |
def __init__(self, value=None): | |
self.head = None | |
if value is not None: | |
self.head = self.Node(value) | |
self.tail = self.head | |
self.count = 0 | |
if value is not None: | |
self.countUp() | |
def append(self, value): | |
if self.head is None: | |
self.head = self.Node(value) | |
self.tail = self.head | |
else: | |
self.tail.next = self.Node(value) | |
self.tail = self.tail.next | |
self.countUp() | |
return self.tail.value | |
def appendAtIndex(self, index, value): | |
if index >= self.count: | |
return False | |
new_node = self.Node(value) | |
prev_node = None | |
curr_node = self.head | |
for i in range(index): | |
prev_node = curr_node | |
curr_node = curr_node.next | |
new_node.next = curr_node | |
prev_node.next = new_node | |
self.countUp() | |
return new_node.value | |
def appendAtHead(self, value): | |
new_node = self.Node(value) | |
new_node.next = self.head | |
self.head = new_node | |
self.countUp() | |
return new_node.value | |
def pop(self): | |
popped = None | |
if self.head is None: | |
return None | |
else: | |
if self.head == self.tail: | |
popped = self.head.value | |
self.head = None | |
self.tail = None | |
else: | |
prev_node = None | |
curr_node = self.head | |
for i in range(self.count-1): | |
prev_node = curr_node | |
curr_node = curr_node.next | |
popped = curr_node.value | |
curr_node = None | |
prev_node.next = None | |
self.tail = prev_node | |
self.countDown() | |
return popped | |
def removeAtIndex(self, index): | |
if index >= self.count: | |
return False | |
prev_node = None | |
curr_node = self.head | |
for i in range(index): | |
prev_node = curr_node | |
curr_node = curr_node.next | |
prev_node.next = curr_node.next | |
if index == self.count-1: | |
if prev_node.next is not None: | |
self.tail = prev_node.next | |
else: | |
self.tail = prev_node | |
self.countDown() | |
return curr_node.value | |
def remove(self, value): | |
prev_node = None | |
curr_node = self.head | |
if self.head.value == value: | |
self.head = None | |
for i in range(self.count-1): | |
if curr_node.value == value: | |
prev_node.next = curr_node.next | |
if self.tail == curr_node: | |
if prev_node.next is not None: | |
self.tail = prev_node.next | |
else: | |
self.tail = prev_node | |
self.countDown() | |
return i | |
prev_node = curr_node | |
curr_node = curr_node.next | |
return False | |
def getHeadValue(self): | |
if self.head is None: | |
return None | |
return self.head.value | |
def getTailValue(self): | |
if self.tail is None: | |
return None | |
return self.tail.value | |
def getValueAtindex(self, index): | |
curr_node = self.head | |
for i in range(index): | |
curr_node = curr_node.next | |
return curr_node.value | |
def getIndexForValue(self, value): | |
curr_node = self.head | |
for i in range(self.count-1): | |
if curr_node.value == value: | |
return i | |
curr_node = curr_node.next | |
return False | |
def __str__(self): | |
out = "" | |
curr_node = self.head | |
for i in range(self.count): | |
out += str(curr_node.value) + " " | |
curr_node = curr_node.next | |
return out | |
def countDown(self): | |
self.count -= 1 | |
def countUp(self): | |
self.count += 1 | |
class Node: | |
next = None | |
value = None | |
def __init__(self, value): | |
self.value = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment