From dc66ea61f6c9d030a552516efc7c891d7b9e7c0b Mon Sep 17 00:00:00 2001 From: Daniel Moix <dwm69@drexel.edu> Date: Wed, 16 Aug 2023 08:02:02 -0400 Subject: [PATCH] Adding files for Week 8 lecture --- cs172/Lect07/hello.py | 0 cs172/Lect07/linkedlist.py | 51 ++++++++++++++++++++++++++++++++++++++ cs172/Lect07/node.py | 31 +++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 cs172/Lect07/hello.py create mode 100644 cs172/Lect07/linkedlist.py create mode 100644 cs172/Lect07/node.py diff --git a/cs172/Lect07/hello.py b/cs172/Lect07/hello.py new file mode 100644 index 0000000..e69de29 diff --git a/cs172/Lect07/linkedlist.py b/cs172/Lect07/linkedlist.py new file mode 100644 index 0000000..6acfaba --- /dev/null +++ b/cs172/Lect07/linkedlist.py @@ -0,0 +1,51 @@ +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 diff --git a/cs172/Lect07/node.py b/cs172/Lect07/node.py new file mode 100644 index 0000000..195b3b3 --- /dev/null +++ b/cs172/Lect07/node.py @@ -0,0 +1,31 @@ + +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 -- GitLab