-
Notifications
You must be signed in to change notification settings - Fork 0
/
dLinkdList.py
83 lines (67 loc) · 1.54 KB
/
dLinkdList.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
head = None
class Node:
def __init__(self, value):
self.value = value
self.pre = None
self.post = None
def insert():
global head
value = raw_input('insert the value for the node')
if not head:
head = Node(value)
else:
currNode = Node(value)
head.pre = currNode
currNode.post = head
head = currNode
def search():
value = raw_input('insert the value')
currNode = head
while currNode.post:
if currNode.value == value:
print 'value found'
return currNode
currNode = currNode.post
if currNode.value == value:
print ('value found')
return currNode
print('value not found')
def delete():
global head
node = search()
if node.pre:
node.pre.post = node.post
else:
head = node.post
if node.post:
node.post.pre = node.pre
node.post = None
node.pre = None
def display():
currNode = head
while currNode.post:
print(currNode.value)
currNode = currNode.post
print(currNode.value)
chooser = {
'i': insert,
'd': display,
's': search,
'r': delete
}
def menu():
while True:
print
print('*'*80)
print('i to insert')
print('d to display')
print('s to search')
print('r to delete')
print('e to exit')
print('*'*80)
option = raw_input()
if chooser.get(option):
chooser[option]()
elif option == 'e':
break
menu()