forked from albertmundu/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.py
115 lines (101 loc) · 2.92 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
'''
@Description: Simple Singly Linked List implementation in python
@Author: Albert Mundu, Bhumil Sarvaiya
'''
class Node(object):
def __init__(self,data=None,next_node=None):
self.data=data
self.next_node=next_node
class LinkedList(object):
def __init__(self,head=None,prev=None):
self.head=head
self.prev=None
def insert(self,data):
if(self.head==None):
new_node=Node(data)
new_node.next_node=None
self.head=new_node
self.prev=new_node
else:
new_node=Node(data)
new_node.next_node=None
self.prev.next_node=new_node
self.prev=new_node
def delete(self):
self.head=self.head.next_node
def clean(self):
self.head=None
def refined_delete(self):
if(self.head==None):
pass
elif(self.head.next_node==None):
self.head=None
else:
self.head=self.head.next_node
def display(self):
tmp=self.head
while tmp!=None:
print tmp.data,
tmp=tmp.next_node
print ""
def get_first_element(self):
tmp=self.head
return tmp.data
def get_first_element_refined(self):
if(self.head==None):
return -1
tmp=self.head
return tmp.data
def get_last_element(self):
tmp=self.head
while tmp!=None:
data=tmp.data
tmp=tmp.next_node
return data
def find_min(self,x):
minimum=10000
tmpy=self.head
tmpx=x.head
while tmpy!=None:
if(tmpy.data<minimum):
minimum=tmpy.data
x=tmpx.data
tmpy=tmpy.next_node
tmpx=tmpx.next_node
return (minimum,x)
def find_max(self,x):
maximum=0
tmpy=self.head
tmpx=x.head
while tmpy!=None:
if(tmpy.data>maximum):
maximum=tmpy.data
x=tmpx.data
tmpy=tmpy.next_node
tmpx=tmpx.next_node
return (maximum,x)
def find_mid(self,x,window_size):
tmpx=x.head
tmpy=self.head
for i in range(int(window_size/2)):
y_value=tmpy.data
x_value=tmpx.data
tmpy=tmpy.next_node
tmpx=tmpx.next_node
return (y_value,x_value)
def find_mid_refined(self,x,window_size):
tmpx=x.head
tmpy=self.head
first=tmpx.data
for i in range(int(window_size/2)):
y_value=tmpy.data
x_value=tmpx.data
if((x_value-first)>int(window_size/2)):
return (previous_y,previous_x)
previous_x=x_value
previous_y=y_value
if(tmpx.next_node==None):
return (previous_y,previous_x)
tmpy=tmpy.next_node
tmpx=tmpx.next_node
return (y_value,x_value)