-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.py
More file actions
43 lines (31 loc) · 1.05 KB
/
LinkedList.py
File metadata and controls
43 lines (31 loc) · 1.05 KB
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
# Node class
class Node:
def __init__(self, data):
self.data = data
self.next = None
# LinkedList class
class LinkedList:
# Creating an empty list with an empty head
def __init__(self):
self.head = None
# Push Function to add a node to the LinkedList
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
if __name__ == '__main__':
linkedlist = LinkedList()
# Loop to let user add nodes to the list
newData = 0
while newData != "fin":
newData = input("Enter the number you want to add to the list, enter \"fin\" when you've finished:")
if newData != "fin":
linkedlist.push(newData)
print(newData + " is added")
# Loop that prints out each node
currentNode = linkedlist.head
print("All nodes off the Linked List:")
while currentNode is not None:
data = currentNode.data
print(data)
currentNode = currentNode.next