forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
157 lines (126 loc) · 3.62 KB
/
linked_list.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'''
Problem Statement :
Linked List : A linked list is a data structure used for storing collections of data.
A linked list has the following properties.
• Successive elements are connected by pointers
• The last element points to NULL
• Can grow or shrink in size during execution of a program
• Can be made just as long as required (until systems memory exhausts)
• Does not waste memory space (but takes some extra memory for pointers). It allocates memory as list grows.
Main Linked Lists Operations :
• Insert: inserts an element into the list
• Delete: removes and returns the specified position element from the list
Auxiliary Linked Lists Operations :
• Delete List: removes all elements of the list (disposes the list)
• Count: returns the number of elements in the list
• Find nth Node from the end of the list
'''
# Node structure having 2 fields : data and next(points next node in the list)
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self, *args):
self.head = Node()
for value in args:
self.insert(value)
# this method have time complexity : O(n)
def insert(self, data):
''' this method takes an argument append at the end of list '''
try:
temp = self.head
new = Node(data)
while temp.next != None:
temp = temp.next
temp.next = new
return
except Exception as e:
raise e
# this method have time complexity : O(n)
def display(self):
''' this method print all available data present in the list
and also return the data in list format '''
try:
elements = []
temp = self.head
while temp.next != None:
temp = temp.next
elements.append(temp.data)
print(elements)
return elements
except Exception as e:
raise e
# this method have time complexity : O(n)
def delete(self, position):
''' this method takes an index position (start from 0) and remove that index element and return that value '''
try:
position = int(position)
if position<0 or position>=self.count():
raise 'Error : Invalid index Position !!!'
else:
cur = self.head
index = 0
while True:
previous_node = cur
cur = cur.next
if index == position:
previous_node.next = cur.next
return cur.data
index += 1
except Exception as e:
raise e
# this method have time complexity : O(1)
def delete_all(self):
''' this method removes all elements present in the list '''
self.head = Node()
# this method have time complexity : O(n)
def count(self):
''' this method returns the length of the list '''
temp = self.head
total_items = 0
while temp.next != None:
total_items += 1
temp = temp.next
return total_items
def get_Nth_last(self, position):
''' this method returns the value of specified position from the last '''
try:
position = int(position)
if position<0:
raise 'Error : Invalid index Position !!!'
total = self.count()
ans = self.__get(total-position)
return ans
except Exception as e:
raise e
# this is a special method of the class...
def __get(self, position):
if position<0:
raise 'Error : Invalid index Position !!!'
cur = self.head
index = 0
while True:
cur = cur.next
if index == position:
return cur.data
index += 1
'''
# Test Case :
ls = LinkedList('sujoy','aot','mechanical engineering')
ls.insert(10)
ls.insert(20)
ls.insert(30)
ls.insert(40)
ls.display()
print(ls.delete(0))
print(ls.count())
ls.display()
print(ls.get_Nth_last(5))
# OUTPUT :
['sujoy', 'aot', 'mechanical engineering', 10, 20, 30, 40]
sujoy
6
['aot', 'mechanical engineering', 10, 20, 30, 40]
mechanical engineering
'''