This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.py
62 lines (55 loc) · 1.77 KB
/
LinkedList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# ---------------------------
# - Ryan Williamson -
# - Advanced Higher Project -
# ---------------------------
"""This class contains the data and a pointer to the nextNode"""
class Node:
"""Class Constructor"""
def __init__(self, data):
self.data = data
self.nextNode = None
"""This class contains the functionality and variables for a Linked List"""
class LinkedList:
"""Class Constructor"""
def __init__(self):
self.length = 0
self.head = None
self.tail = None
"""
Returns the length of the Linked List using python's built-in
len() function
"""
def __len__(self):
return self.length
"""Add an item to the end of the linked list"""
def Append(self, data):
self.length += 1
# Create new Node
current = Node(data)
# Set the Node after current to the current head Node
current.nextNode = self.head
# Change current head Node to the current node
self.head = current
"""Linear search through the linked list"""
def Search(self, value):
# Start at the head Node
current = self.head
found = False
# While there is a node and not found value
while ((current != None) and not(found)):
if value == current.data:
found = True
# Move node forward
current = current.nextNode
return found
"""Display the list as a series of print statements"""
def debugOutput(self):
current = self.head
while (current != None):
print(str(current.data))
current = current.nextNode
"""Clear the linked list of all values"""
def Clear(self):
self.head = None
self.tail = None
self.length = 0