-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefrence.py
More file actions
75 lines (59 loc) · 2.36 KB
/
refrence.py
File metadata and controls
75 lines (59 loc) · 2.36 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# A Node class represnting each element in the linked list:
class Node: # Other nodes.
def __init__(self, data):
self.data = data # Store the data.
self.next = None # Pointer to the next node (defaults to None)
# Linked list class:
class LinkedList:
def __init__(self):
self.head = None # The head node.
def append(self, data):
if self.head is None: # The first node gets assigned as the head.
new_node = Node(data)
self.head = new_node
return
# Else/after a head is assigned
# keep traversing and appending new nodes.
new_node = Node(data)
current = self.head
while current.next: # While there is still a pointer (tails have none):
current = current.next # Assign the data to the next node.
current.next = new_node
def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None") # By the time you reach the end there is no more pointer.
# Appending a node(but it goes to the back of the list)
def prepend(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def delete(self, key): # key is the val of the node to be deleted.
current = self.head # Start at the head.
prev = None # There should be no previous node when your on the head.
# Case 1: List is empty
if not current: # If there is no head.
return # Return so.
# Case 2: The node to delete is the head.
if current.data == key: # If the head's data is the one to be deleted
self.head = current.next # Switch to the next node and make that the new head.
return
# Traverse the list to find the node to delete
while current and current.data != key: # While there's a head and the data doesn't match:
prev = current # The previous node is now the head.
current = current.next
# If the key was not found, do nothing
if not current:
return
# Unlink the node from the list
if current:
prev.next = current.next
test = LinkedList()
test.prepend(3)
test.prepend(2)
test.prepend(5)
test.append(7)
test.display()
test.delete(2)