-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
71 lines (65 loc) · 1.92 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
class Node(object):
def __init__(self, data=None, next=None):
self.data=data
self.next=next
class Linked_list(object):
def __init__(self, head=None, tail=None):
self.head=head
self.tail=tail
def show_elements(self):
print 'showing list elements'
current = self.head
while current is not None:
print current.data, "-->",
current = current.next
print None
def append(self, data):
next = Node(data, None)
if self.head == None:
self.head=self.tail=next
else:
self.tail.next = next
self.tail=next
def remove(self, next_value):
current=self.head
previous = None
while current is not None:
if current.data== next_value:
if previous is not None:
previous.next = current.next
else :
self.head=current.next
previous = current
current = current.next
def search(self, node_data):
current = self.head
node_point = 1
while current is not None:
if current.data == node_data:
print 'Element found at %s' % (node_point)
break
else :
current = current.next
node_point+=1
# below function need to be modified .. it is replacing , it should insert
def insert_arbitratry(self, position, new):
previous = current = self.head
new = Node(new, next)
while position is not 0:
current=current.next
previous=previous.next
position-=1
print position, previous, current
previous.next=new.next
new.next= current.next
s=Linked_list()
s.append(31)
s.append(2)
s.append(23)
s.insert_arbitratry(2, 12)
s.append(25)
s.show_elements()
s.remove(23)
s.append(55)
s.show_elements()
s.search(55)