在c/c++中,通常採用「指標+結構體」來實現鍊錶;而在python中,則可以採用「引用+類」來實現鍊錶。
節點類:
class鍊錶類:node:
def__init__
(self, data):
self.data =data
self.next = none
classlinkedlist:
def__init__
(self):
self.head =none
self.tail = none
link_list = linkedlist()
defis_empty(self):
return self.head is none
defnode =node(data)
if self.head is
none:
self.head =node
self.tail =node
else
: self.tail.next =node
self.tail =node
def鍊錶的頭結點head 和 尾節點tail 都屬於node.iter(self):
ifnot
iter.head:
return
cur =self.head
yield
cur.data
while
cur.next:
cur =cur.next
yield
cur.data
#先判斷是不是空鍊錶,yield head.data 再用while迴圈遍歷
insert:先將要插入的節點的next指向之後鍊錶的head,然後將之前鍊錶的next指向 將要插入的節點。
definsert(self, idx, value):
cur =self.head
cur_idx =0
if cur is
none:
raise exception('
that list is and empty list!')
while cur_idx < idx-1:
cur =cur.next
if cur is
none:
raise exception('
list length less than index!')
cur_idx += 1node =node(value)
node.next =cur.next
cur.next =node
if node.next is
none:
self.tail = node
defremove(self, idx):
cur =self.head
cur_idx =0
#空指標
if self.head =none:
raise exception('
this is an empty list')
while cur_idx < idx-1:
cur =cur.next
#給出的索引大於鍊錶的長度
if cur is
none:
raise exception('
list length less than index')
cur_idx +=1
if idx == 0: #
當刪除第乙個節點時
self.head =cur.next
cur =cur.next
return
if self.head is self.tail: #
當只有乙個節點時
self.head =none
self.tail =none
return
cur.next =cur.next.next
if cur.next is none: #
當刪除最後乙個節點時
self.tail =cur
defsize(self):
i =0
cur =self.head
if current is
none:
return
'the list is an empty list
'while cur.next is
notnone:
i +=1cur =cur.next
return i
def單鏈表逆置search(self, item):
current =self.head
found =false
while current is
not none and
notfound:
if current.data ==item:
found =true
else
: current =current.next
return found
1,迭代
#比較形象的圖-*- coding: utf-8 -*-
#!/bin/env python
#python2.7
class
node(object):
def__init__
(self):
self.value =none
self.next =none
def__str__
(self):
return
str(self.value)
defreverse_list(head):
ifnot head or
nothead.next:
return
head
pre =none
while
head:
next = head.next #
快取當前節點的向後指標,待下次迭代用
head.next = pre #
關鍵:把當前節點向前指標(pre)作為當前節點的向後指標
pre = head #
把當前指標賦值給 下次迭代 節點的 向前指標
head = next #
作為下次迭代時的(當前)節點
return pre #
返回頭指標,頭指標就是迭代最後一次的head(賦值給類pre)
if__name__ == '
__main__':
three =node()
three.value = 3two =node()
two.value = 2two.next =three
one =node()
one.value = 1one.next =two
head =node()
head.value =0
head.next =one
newhead =reverse_list(head)
while
newhead:
newhead.value
newhead = newhead.next
2,遞迴
#臨界點:head.next為none
#先遞迴到 把最後乙個節點指向 newhead
#然後一步步從後往前逆置
defreverse_recursion(head):
ifnot head or
nothead.next:
return
head
new_head =reverse_recursion(head.next)
head.next.next =head
head.next =none
return new_head
python鍊錶
class node def init self,data 要存的資料 self.data data 指向下乙個節點的指標 self.next none class linkedlist def init self 鍊錶長度 self.size 0 鍊錶的頭部 self.head none 鍊錶的尾...
鍊錶(python)
class node def init self,value none next none self.value value self.next next class linkedlist def init self self.head node self.tail none self.length...
python鍊錶 鍊錶重新排序
輸入 1 2 3 4 5 6 7 輸出 1 7 2 6 3 5 4 或者輸入 1 2 3 4 5 6 輸出 1 6 2 5 3 4 思路 1.將1 2 3 4 5 6 7分成 1 2 3 與 4 5 6 7,將後半部分逆序7 6 5 4 2.合併1 2 3與7 6 5 4 為1 7 2 6 3 5 ...