-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.py
121 lines (105 loc) · 3.34 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
# Insert a node at the beginning of the linked list
def insertAtBeginning(self, data):
newNode = Node(data)
newNode.next = self.head
self.head = newNode
# Insert a node at the end of the linked list
def insertAtEnd(self, data):
newNode = Node(data)
if self.head is None:
self.head = newNode
return
lastNode = self.head
while lastNode.next:
lastNode = lastNode.next
lastNode.next = newNode
# Insert a node in ascending order
def insertInAscendingOrder(self, data):
newNode = Node(data)
if self.head is None:
self.head = newNode
return
elif self.head.data > data:
newNode.next = self.head
self.head = newNode
return
tempNode = self.head
while tempNode.next and tempNode.next.data < data:
tempNode = tempNode.next
newNode.next = tempNode.next
tempNode.next = newNode
# Delete the first node of the linked list
def deleteFirstNode(self):
if self.head is None:
return
tempNode = self.head
self.head = tempNode.next
tempNode = None
# Delete a node before a specific position
def deleteBeforePosition(self, position):
if self.head is None or position == 0:
return
tempNode = self.head
if position == 1:
self.head = tempNode.next
tempNode = None
return
for i in range(position - 2):
if tempNode is None:
return
tempNode = tempNode.next
if tempNode is None or tempNode.next is None:
return
nodeToDelete = tempNode.next
tempNode.next = nodeToDelete.next
nodeToDelete = None
# Delete a node after a specific position
def deleteAfterPosition(self, position):
if self.head is None:
return
tempNode = self.head
if position == 0:
self.head = tempNode.next
tempNode = None
return
for i in range(position - 1):
if tempNode is None:
return
tempNode = tempNode.next
if tempNode is None or tempNode.next is None:
return
nodeToDelete = tempNode.next
tempNode.next = nodeToDelete.next
nodeToDelete = None
# Print the linked list
def printLinkedList(self):
currentNode = self.head
while currentNode:
print(currentNode.data, end=" ")
currentNode = currentNode.next
# Create a linked list object
linkedlist = LinkedList()
# Insert nodes at the beginning
linkedlist.insertAtBeginning(3)
linkedlist.insertAtBeginning(2)
linkedlist.insertAtBeginning(1)
# Insert nodes at the end
linkedlist.insertAtEnd(4)
linkedlist.insertAtEnd(5)
# Insert node in ascending order
linkedlist.insertInAscendingOrder(2.5)
linkedlist.insertInAscendingOrder(1.5)
# Print the linked list
linkedlist.printLinkedList()
#linkedlist.deleteFirstNode()
#linkedlist.printLinkedList()
#linkedlist.deleteAfterPosition(2)
#linkedlist.deleteBeforePosition(5)
#linkedlist.printLinkedList()