Last active
January 29, 2017 09:06
-
-
Save JoshZastrow/9a99871e8e731e5286f6df0df3700801 to your computer and use it in GitHub Desktop.
LinkedList
This file contains 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 Node(object): | |
def __init__(self, initdata=None, next=None): | |
self.data = initdata | |
self.next = next | |
def getData(self): | |
return self.data | |
def getNext(self): | |
return self.next | |
def setData(self, newdata): | |
self.data = newdata | |
def setNext(self, newnode): | |
self.next = newnode | |
class LinkedList(object): | |
"""Linked List""" | |
def __init__(self, head=None): | |
self.head = head | |
def insert(self, data): | |
# Create a node object | |
new_node = Node(data) | |
# Set the current head to be the next node | |
new_node.setNext(self.head) | |
# Set the node object to be the new head | |
self.head = new_node | |
def size(self): | |
current = self.head | |
count = 0 | |
while current: | |
count += 1 | |
current = current.getNext() | |
return count | |
def search(self, data): | |
current = self.head | |
found = False | |
while current and found is False: | |
if current.getData() == data: | |
found = True | |
else: | |
current = current.getNext() | |
if current is None: | |
raise ValueError("Data is not in list") | |
return current | |
def delete(self,data): | |
if self.head == None: return | |
current = self.head | |
lastNode = None | |
found = False | |
while current and found is False: | |
if current.getData() == data: | |
found = True | |
else: | |
lastNode = current | |
current = current.getNext() | |
if current is None: | |
raise ValueError("Data is not in list") | |
# Special case: Head node is the one we want to delete | |
if lastNode is None: | |
self.head = current.getNext() | |
else: | |
lastNode.setNext(current.getNext()) | |
def append(self, data): | |
if self.head == None: | |
Node(data) | |
return | |
current = self.head | |
while current: | |
current = current.getNext() | |
current.next = Node(data) | |
temp = LinkedList() | |
temp.insert(93) | |
This file contains 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 Node: | |
def __init__(self, cargo=None, next=None): | |
self.cargo = cargo | |
self.next = next | |
def __str__(self): | |
return str(self.cargo) | |
def print_backward(self): | |
if self.next != None: | |
tail = self.next | |
tail.print_backward() | |
print self.cargo | |
def printList(node): | |
while node: | |
print node, | |
node = node.next | |
class LinkedList: | |
def __init__(self): | |
self.length = 0 | |
self.head = None | |
def printReversed(self): | |
# Base Case | |
if self.head != None: | |
self.head.print_backward() | |
print head, | |
def addFirst(self, cargo): | |
node = Node(cargo) | |
node.next = self.head | |
self.head = node | |
self.length = self.length + 1 | |
# Create Nodes | |
node1 = Node(12) | |
node2 = Node(32) | |
node3 = Node(43) | |
# Link the Nodes | |
node1.next = node2 | |
node2.next = node3 | |
# print the Linked list | |
print "Linked List:\t", | |
printList(node1) | |
print "Reversed:\t\t", | |
printListReversed(node1) | |
This file contains 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 Node: | |
def __init__(self, data=None, next=None): | |
self.data = data | |
self.next = next | |
def __str__(self): | |
return str(self.data) | |
def getNext(self): | |
return self.next | |
def setNext(self, newNext): | |
self.next = newNext | |
def setData(self, newData): | |
self.data = newData | |
class LinkedList: | |
def __init__(self, head=None): | |
self.head = head | |
def isEmpty(self): | |
return self.head == None | |
def add(self, newdata): | |
if self.isEmpty: | |
self.head = Node(newdata) | |
else: | |
newNode = Node(newdata) | |
newNode.setNext(self.head) | |
self.head = newNode | |
def size(self): | |
current = self.head | |
count = 0 | |
while current != None: | |
count += 1 | |
current = current.getNext() | |
return count | |
def search(self, item): | |
if self.head == None: return | |
found = False | |
current = self.head | |
while current and not found: | |
if current.data == item: | |
found = True | |
else: | |
current = current.getNext() | |
return found | |
def replace(self, item, newitem): | |
if self.head == None: return | |
found = False | |
current = self.head | |
while current and not found: | |
if current.data == item: | |
current.setData(newitem) | |
found = True | |
else: | |
current = current.next | |
return | |
def remove(self, item): | |
if self.head == item: | |
head = head.next | |
return | |
found = False | |
current = self.head | |
previous = None | |
while current and not found: | |
if current.next.data == item: | |
found = True | |
current.next = current.next.next | |
return | |
else: | |
current = current.getNext() | |
temp = Node(5) | |
print(temp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment