Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac1e6af

Browse files
author
Your Name
committed
About LinkedList.
0 parents commit ac1e6af

File tree

2 files changed

+369
-0
lines changed

2 files changed

+369
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def binary_search(list, item):
2+
low = 0
3+
high = len(list) - 1
4+
5+
while low <= high:
6+
mid = (low+high) // 2
7+
guess = list[mid]
8+
if guess == item:
9+
return print('Yeah,found it:%d'%mid)
10+
if guess > item:
11+
high = mid - 1
12+
else:
13+
low = mid + 1
14+
15+
return print('Oops,there is no one.')
16+
'''for example'''
17+
my_list = [1,4,8,9,22,55,65,89,321,2345]
18+
19+
binary_search(my_list, 2345)
Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
1+
#单向链表
2+
#节点的实现
3+
class SingleNode(object):
4+
5+
def __init__(self, item):
6+
self.item = item
7+
self.next = None
8+
9+
#单链表的实现
10+
class SingleLinkedList(object):
11+
12+
def __init__(self):
13+
self._head = None
14+
15+
def is_empty(self):
16+
return self._head == None
17+
18+
def length(self):
19+
cur = self._head
20+
count = 0
21+
22+
while cur != None:
23+
count += 1
24+
cur = cur.next
25+
26+
return count
27+
28+
def travel(self):
29+
cur = self._head
30+
31+
while cur != None:
32+
print cur.item
33+
cur = cur.next
34+
35+
def add(self, item):
36+
#创建一个保存item值的节点
37+
node = SingleNode(item)
38+
#将新节点的链接域指向头节点
39+
node.next = self._head
40+
"""_head相当于q变量,让_head变量指向新节点"""
41+
self._head = node
42+
43+
def append(self, item):
44+
node = SingleNode(item)
45+
46+
if self.is_empty():
47+
self._head = node
48+
49+
else:
50+
cur = self._head
51+
52+
while cur.next != None:
53+
cur = cur.next
54+
55+
cur.next = node
56+
"""从头遍历到要插入的位置,将新节点的链接域
57+
指向插入位置的下一个节点,将。。。找到位置,然后
58+
打断这条链,强行把新节点插入(把位置的前一个节点
59+
的链接域指向新节点,新节点的链接域指向下一个节点的
60+
元素域)"""
61+
def insert(self, pos, item):
62+
if pos <= 0:
63+
self.add(item)
64+
elif pos > (self.length() - 1):
65+
self.append(item)
66+
else:
67+
node = SingleNode(item)
68+
count = 0
69+
pre = self._head
70+
71+
while count < (pos-1):
72+
count += 1
73+
pre = pre.next
74+
75+
node.next = pre.next
76+
pre.next = node
77+
78+
def remove(self, item):
79+
cur = self._head
80+
pre = None
81+
82+
while cur != None:
83+
if cur.item == item:
84+
"""如果头节点为目标节点,则将头指针
85+
指向头节点的下一个节点"""
86+
if not pre:
87+
"""_head是头指针,cur和pre是两个
88+
节点"""
89+
self._head = cur.next
90+
else:
91+
pre.next = cur.next
92+
break
93+
else:
94+
pre = cur
95+
cur = cur.next
96+
97+
def search(self, item):
98+
if self.is_empty():
99+
return
100+
cur = self._head
101+
while cur != None:
102+
if cur.item == item:
103+
"""print cur.item
104+
break"""
105+
return True
106+
cur = cur.next
107+
return False
108+
109+
110+
"""单向循环链表"""
111+
class Node(object):
112+
def __init__(self, item):
113+
self.item = item
114+
self.next = None
115+
116+
117+
class SinCycLinkedList(object):
118+
119+
def __init__(self):
120+
self._head = None
121+
122+
def is_empty(self):
123+
return self._head == None
124+
125+
def length(self):
126+
if is_empty():
127+
return 0
128+
cur = self._head
129+
count = 0
130+
if cur.next == self._head:
131+
return 1
132+
else:
133+
while cur != self._head:
134+
count += 1
135+
cur = cur.next
136+
return count
137+
138+
def length(self):
139+
if is_empty():
140+
return 0
141+
cur = self._head
142+
count = 1
143+
144+
while cur.next != self._head:
145+
count += 1
146+
cur = cur.next
147+
return count
148+
149+
def travel(self):
150+
if self.is_empty():
151+
return
152+
cur = self._head
153+
print cur.item
154+
while cur.next != self._head:
155+
cur = cur.next
156+
print cur.item
157+
158+
def add(self, item):
159+
node = SingleNode(item)
160+
161+
if self.is_empty():
162+
self._head = node
163+
node.next = self._head
164+
else:
165+
node.next = self._head
166+
cur = self._head
167+
while cur.next != self._head:
168+
cur = cur.next
169+
cur.next = node
170+
self._head = node
171+
172+
def append(self, item):
173+
node = Node(item)
174+
if self.is_empty():
175+
self._head = node
176+
node.next = self._head
177+
else:
178+
cur = self._head
179+
while cur.next != self._head:
180+
cur = cur.next
181+
cur.next = node
182+
node.next = self._head
183+
184+
def insert(self, pos, item):
185+
if pos <= 0:
186+
self.add(item)
187+
elif pos > (self.length() - 1):
188+
self.append(item)
189+
else:
190+
node = Node(item)
191+
cur = self._head
192+
count = 0
193+
while count < (pos-1):
194+
count += 1
195+
cur = cur.next
196+
node.next = cur.next
197+
cur.next = node
198+
199+
def remove(self, item):
200+
if self.is_empty():
201+
return
202+
cur = self._head
203+
pre = None
204+
if cur.item == item:
205+
if cur.next != self._head:
206+
while cur.next != self._head:
207+
cur = cur.next
208+
cur.next = self._head.next
209+
self._head = self._head.next
210+
else:
211+
self._head = None
212+
else:
213+
pre = self._head
214+
while cur.next != self._head:
215+
if cur.item ==item:
216+
pre.next = cur.next
217+
return
218+
else:
219+
pre = cur
220+
cur = cur.next
221+
if cur.item == item:
222+
pre.next = cur.next
223+
224+
def search(self, item):
225+
if self.is_empty():
226+
return False
227+
cur = self._head
228+
if cur.item == item:
229+
return True
230+
while cur.next != self._head:
231+
cur = cur.next
232+
if cur.item == item:
233+
return True
234+
return False
235+
236+
"""双向链表"""
237+
class Node(object):
238+
def __init__(self, item):
239+
self._head = item
240+
self.next = None
241+
self,prev = None
242+
243+
244+
class DLinkList(object):
245+
def __init__(self):
246+
self._head = None
247+
248+
def is_empty(self):
249+
return self._head == None
250+
251+
def length(self):
252+
cur = self._head
253+
count = 0
254+
while cur != None:
255+
count += 1
256+
cur = cur.next
257+
return count
258+
259+
def travel(self):
260+
cur = self._head
261+
while cur != None:
262+
print cur.item
263+
cur = cur.next
264+
265+
def add(self, item):
266+
node = Node(item)
267+
if self.is_empty():
268+
self._head = node
269+
else:
270+
node.next = self._head
271+
self._head.prev = node
272+
self._head = node
273+
274+
def append(self, item):
275+
node = Node(item)
276+
if self.is_empty():
277+
self._head = node
278+
else:
279+
cur = self._head
280+
while cur.next != None:
281+
cur = cur.next
282+
cur.next = node
283+
node.prev = cur
284+
285+
def search(self, item):
286+
cur = self._head
287+
while cur != None:
288+
if cur.item == item:
289+
return True
290+
cur = cur.next
291+
return False
292+
293+
def insert(self, pos, item):
294+
if pos <= 0:
295+
self.add(item)
296+
elif pos > (self.length(item)-1):
297+
self.append(item)
298+
else:
299+
node = Node(item)
300+
cur = self._head
301+
count = 0
302+
while count < (pos-1):
303+
count += 1
304+
cur = cur.next
305+
node.prev = cur
306+
node.next = cur.next
307+
cur.next.prev = node
308+
cur.next = node
309+
310+
def remove(self, item):
311+
if self.is_empty():
312+
return
313+
else:
314+
cur = self._head
315+
if cur.item == item:
316+
if cur.next == None:
317+
self._head None
318+
else:
319+
cur.next.prev = None
320+
self._head = cur.next
321+
return
322+
while cur != None:
323+
if cur.item == item:
324+
cur.prev.next = cur.next
325+
cur.next.prev = cur.prev
326+
break
327+
cur = cur.next
328+
329+
330+
331+
332+
333+
334+
335+
336+
337+
338+
339+
340+
341+
342+
343+
344+
345+
346+
347+
348+
349+
350+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /