-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
55 lines (48 loc) · 1.16 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
class Node:
def __init__(self,data):
self.data = data
self.prev = None
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add(self,data):
node = Node(data)
if self.head == None:
self.head = node
else:
node.next = self.head
node.next.prev = node
self.head = node
def __str__(self):
p = self.head
s = ''
while p.next != None:
s += p.data
p = p.next
s += p.data
return s
def search_data(self,data):
n = self.head
while n.next != None:
if n.data == data:
return True
n = n.next
return False
def remove_data(self,data):
n = self.head
while n.next != None:
if n.data == data:
n.prev.next = n.next
n.next.prev = n.prev
break
n = n.next
print "%s not found in linked list"%(data)
l = LinkedList()
l.add('x')
l.add('y')
l.add('z')
print str(l)[::-1]
print l.search_data('t')
l.remove_data('t')
print l