-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
146 lines (113 loc) · 3.58 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
from __future__ import print_function
class ChainNode():
def __init__(self, ele, nxt):
self.ele = ele
self.nxt = nxt
ChainNode(None, None)
class Chain():
def __init__(self, source_list = None):
if not source_list:
self.first_node = None
self.list_size = 0
return
self.list_size = source_list.list_size
if self.list_size == 0:
self.first_node = None
return
src_node = source_list.first_node
self.first_node = ChainNode(src_node.ele, None)
src_node = src_node.next
target_node = self.first_node
while not src_node:
target_node.nxt = ChainNode(src_node.ele, None)
target_node = target_node.nxt
src_node = src_node.nxt
target_node.nxt = None
def check_index(self, idx):
return 0 <= idx <= self.list_size
def empty():
return list_size == 0
def size():
return list_size
def get(self, index):
if check_index:
current_node = self.first_node
idx = 0
while idx != index:
current_node = current_node.nxt
idx += 1
return current_node.ele
def index_of(self, ele):
current_node = this.first_node
idx = 0
while current_node and current_node.ele != ele:
current_node = current_node.nxt
idx += 1
if current_node:
return idx
else:
return None
def erase(self, index):
if check_index(index):
if index == 0:
self.first_node = self.first_node.nxt
else:
prev_node = self.first_node
for trk in range(index):
prev_node = prev_node.nxt
prev_node.nxt = prev_node.nxt.nxt
self.list_size -= 1
def insert(self, index = None, ele = None):
if index < 0 or index > self.list_size + 1:
print("wont be able to insert")
return
if index == 0:
self.first_node = ChainNode(ele, self.first_node)
# print('first_node ', first_node)
# print()
else:
prev_node = self.first_node
for trk in range(index):
prev_node = prev_node.nxt
# see here we are doing initialization and
# assigning next in the constructor itself
prev_node.nxt = ChainNode(ele, prev_node.nxt)
def output(self):
curr_node = self.first_node
# print('curr_node', curr_node)
# print()
while curr_node != None:
print(curr_node.ele, end = ' ')
# print()
curr_node = curr_node.nxt
print()
chain = Chain()
def inserter():
print('please insert index and element')
print()
index, ele = raw_input().split(' ')
index = int(index)
# print(' index', index)
ele = int(ele)
chain.insert(index, ele)
menu = {
'c': chain.check_index,
's': chain.size,
'g': chain.get,
'in': chain.index_of,
'e': chain.erase,
'i': inserter,
'o': chain.output
}
while(True):
print('c for check_index')
print('s for chain size')
print('g for get')
print('in for index of')
print('e for erase')
print('i for insert')
print('o for output')
choice = raw_input('please choose one of the above choice: ')
if choice == 'x':
break
menu[choice]()