Skip to content
Snippets Groups Projects
Commit dc66ea61 authored by Daniel Moix's avatar Daniel Moix
Browse files

Adding files for Week 8 lecture

parent 8180026b
Branches
No related tags found
No related merge requests found
from node import Node
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
def isEmpty(self):
return self.__head is None
def append(self, value):
if self.isEmpty():
self.__head = Node(value)
self.__tail = self.__head
else:
self.__tail.setNext(Node(value))
self.__tail = self.__tail.getNext()
def __str__(self):
#return str(self.__head)
myStr = ''
current = self.__head
while current is not None:
myStr += str(current.getData()) + " --> "
current = current.getNext()
myStr += '🛑'
return myStr
def __len__(self):
if self.isEmpty():
return 0
else:
count = 1
# Traverse
here = self.__head
while here.getNext() is not None:
here = here.getNext()
count += 1
return count
if __name__ == "__main__":
myList = LinkedList()
print(myList.isEmpty())
print(len(myList))
myList.append("A")
print(len(myList))
myList.append("B")
myList.append("C")
print(myList.isEmpty())
print(myList)
\ No newline at end of file
class Node:
def __init__(self, data, next=None):
self.__data = data
self.__next = next
def getData(self):
return self.__data
def getNext(self):
return self.__next
def setNext(self, next):
self.__next = next
def __str__(self):
return "A node containing " + str(self.getData()) + \
" whose next is " + str(self.getNext())
if __name__ == "__main__":
myNode = Node("A")
print(myNode.getData())
print(myNode.getNext())
print(myNode)
yourNode = Node("Z", myNode)
print(yourNode)
#print(yourNode.getNext().getData())
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment