Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
LinkedList implementation
""" LinkedList implementation Author: Jayesh Chandrapal """ class Node: __slots__ = "data", "next" def __init__(self, data, next = None): self.data = data self.next = next def __str__(self): return '[' + self.data + ']' class LinkedList: __slots__ = "front", "back", "currentSize" def __init__(self): self.front = None self.back = None self.currentSize = 0 def prepend(self, item): self.front = Node(item, self.front) if self.back is None: self.back = self.front self.currentSize += 1 def append(self, item): newnode = Node(item) if self.back is None: self.back = newnode else: self.back.next = newnode self.back = newnode if self.front is None: self.front = self.back self.currentSize += 1 def contains(self, item): if self.empty(): return False current = self.front while current is not None: if current.data == item: return True current = current.next return False def remove(self, item): if self.empty(): return False current = self.front previous = None while current is not None: if current.data == item: if previous is None: self.front = current.next else: previous.next = current.next current.next = None self.currentSize -= 1 return True previous = current current = current.next return False def size(self): return self.currentSize def empty(self): return self.currentSize == 0 def clear(self): self.front = None self.back = None self.currentSize = 0 self = LinkedList() def __str__(self): result = "" current = self.front while current is not None: result += "[" + str(current.data) + "]" current = current.next return result def print_reverse(self): print() self._print_reverse(self.front) print() def _print_reverse(self, current = None): if current is None: return self._print_reverse(current.next) print("[" + str(current.data) + "]", end = ' ') def testLinkedList(): result = True mylist = LinkedList() result &= mylist.size() == 0 mylist.append("bob") mylist.append("mob") mylist.append("goes") print(mylist) print("size = " + str(mylist.size())) result &= mylist.size() == 3 print("contains bob: " + str(mylist.contains("bob"))) result &= mylist.contains("bob") mylist.remove("bob") result &= not mylist.contains("bob") print(mylist) print("size = " + str(mylist.size())) print("contains bob: " + str(mylist.contains("bob"))) mylist.remove("bob") result &= not mylist.contains("bob") print("size = " + str(mylist.size())) result &= mylist.size() == 2 print("contains bob: " + str(mylist.contains("bob"))) print(mylist) mylist.prepend("end") result &= mylist.size() == 3 mylist.prepend("final end") result &= mylist.size() == 4 print(mylist) print("size = " + str(mylist.size())) print("contains end: " + str(mylist.contains("end"))) result &= mylist.contains("end") print("contains jay: " + str(mylist.contains("jay"))) result &= not mylist.contains("jay") mylist.clear() result &= mylist.size() == 0 for i in range(100): mylist.append('batman-'+str(i)) result &= mylist.contains('batman-'+str(i)) mylist.print_reverse() print("Tests passed: " + str(result)) if __name__ == "__main__": testLinkedList()
run
|
edit
|
history
|
help
0
Reading XML file to modify
Lab_I_1_25_11_2020
gj
Iterator-1
pip install pyowm==2.7.1
PyRegCapGroup
Sum of subsets (Python)
Prac1_2
Gh
snake water gam correct