## 單向循環鏈表
單鏈表的一個變形是單向循環鏈表,鏈表中最后一個節點的next域不再為None,而是指向鏈表的頭節點。

### 操作
* is_empty() 判斷鏈表是否為空
* length() 返回鏈表的長度
* travel() 遍歷
* add(item) 在頭部添加一個節點
* append(item) 在尾部添加一個節點
* insert(pos, item) 在指定位置pos添加節點
* remove(item) 刪除一個節點
* search(item) 查找節點是否存在
### 實現
~~~
class Node(object):
"""節點"""
def __init__(self, item):
self.item = item
self.next = None
class SinCycLinkedlist(object):
"""單向循環鏈表"""
def __init__(self):
self._head = None
def is_empty(self):
"""判斷鏈表是否為空"""
return self._head == None
def length(self):
"""返回鏈表的長度"""
# 如果鏈表為空,返回長度0
if self.is_empty():
return 0
count = 1
cur = self._head
while cur.next != self._head:
count += 1
cur = cur.next
return count
def travel(self):
"""遍歷鏈表"""
if self.is_empty():
return
cur = self._head
print cur.item,
while cur.next != self._head:
cur = cur.next
print cur.item,
print ""
def add(self, item):
"""頭部添加節點"""
node = Node(item)
if self.is_empty():
self._head = node
node.next = self._head
else:
#添加的節點指向_head
node.next = self._head
# 移到鏈表尾部,將尾部節點的next指向node
cur = self._head
while cur.next != self._head:
cur = cur.next
cur.next = node
#_head指向添加node的
self._head = node
def append(self, item):
"""尾部添加節點"""
node = Node(item)
if self.is_empty():
self._head = node
node.next = self._head
else:
# 移到鏈表尾部
cur = self._head
while cur.next != self._head:
cur = cur.next
# 將尾節點指向node
cur.next = node
# 將node指向頭節點_head
node.next = self._head
def insert(self, pos, item):
"""在指定位置添加節點"""
if pos <= 0:
self.add(item)
elif pos > (self.length()-1):
self.append(item)
else:
node = Node(item)
cur = self._head
count = 0
# 移動到指定位置的前一個位置
while count < (pos-1):
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def remove(self, item):
"""刪除一個節點"""
# 若鏈表為空,則直接返回
if self.is_empty():
return
# 將cur指向頭節點
cur = self._head
pre = None
# 若頭節點的元素就是要查找的元素item
if cur.item == item:
# 如果鏈表不止一個節點
if cur.next != self._head:
# 先找到尾節點,將尾節點的next指向第二個節點
while cur.next != self._head:
cur = cur.next
# cur指向了尾節點
cur.next = self._head.next
self._head = self._head.next
else:
# 鏈表只有一個節點
self._head = None
else:
pre = self._head
# 第一個節點不是要刪除的
while cur.next != self._head:
# 找到了要刪除的元素
if cur.item == item:
# 刪除
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# cur 指向尾節點
if cur.item == item:
# 尾部刪除
pre.next = cur.next
def search(self, item):
"""查找節點是否存在"""
if self.is_empty():
return False
cur = self._head
if cur.item == item:
return True
while cur.next != self._head:
cur = cur.next
if cur.item == item:
return True
return False
if __name__ == "__main__":
ll = SinCycLinkedlist()
ll.add(1)
ll.add(2)
ll.append(3)
ll.insert(2, 4)
ll.insert(4, 5)
ll.insert(0, 6)
print "length:",ll.length()
ll.travel()
print ll.search(3)
print ll.search(7)
ll.remove(1)
print "length:",ll.length()
ll.travel()
~~~
- 系統編程
- 1.進程
- 1.1.fork
- 1.2.多個進程能否修改全局變量
- 1.3多次fork的問題
- 1.4.進程的創建-multiprocessing
- 1.5.進程的創建-Process子類
- 1.6.進程池Pool
- 1.7.進程間通信--Queue
- 2.線程
- 2.1.多線程-Threading
- 2.2.threading注意點
- 2.3.多線程-共享全局變量
- 2.4.線程和進程的對比
- 2.5.同步
- 2.6.互斥鎖
- 2.7.多線程-非共享數據
- 2.8.死鎖
- 2.9.同步應用
- 2.10.生產者與消費者模式
- 2.11.ThreadLocal
- 2.12.異步
- 2.13.GIL的問題
- 網絡編程
- 1.網絡概述-udp
- 1.1.TCP/IP
- 1.2.端口
- 1.3.ip地址
- 1.4.socket簡介
- 1.5.UDP介紹
- 1.6.udp網絡程序-發送數據
- 1.7.udp網絡程序-發送、接收數據
- 1.8.udp網絡程序-端口問題
- 1.9.udp綁定信息
- 2.0.udp網絡通信過程
- 2.1.udp應用:echo服務器
- 2.2.udp應用:聊天室
- 2.3.udp總結
- 2.4.udp綜合-模擬QQ
- 2.TFTP下載和上傳
- 3.TCP/IP
- 3.1.打開瀏覽器訪問百度的過程
- web服務器
- 1.1.MyWebServer.py
- 1.2.MyWebFramework.py
- 正則
- 1.1.re模塊
- 1.2.字符
- 1.3.原始字符串
- 1.4.表示數量
- 1.5.表示邊界
- 1.6.匹配分組
- 1.7.貪婪和非貪婪
- 數據結構和算法
- 1.引入概念
- 1.1.第一次嘗試
- 1.2.算法的提出
- 1.3.第二次嘗試
- 1.4.算法效率衡量
- 1.5.算法分析
- 1.6.常見時間復雜度
- 1.7.python內置類型性能分析
- 1.8.數據結構
- 2.順序表
- 2.1.順序表的形式
- 2.2.順序表的結構和實現
- 2.3.順序表的操作
- 2.4.python中的順序表
- 3.鏈表
- 3.1.單向鏈表
- 3.2.單向循環鏈表
- 3.3.雙向鏈表
- 4.棧
- 4.1.棧的結構實現
- 5.隊列
- 5.1.隊列的實現
- 5.2.雙端隊列
- 6.排序和搜索
- 6.1.冒泡排序
- 6.2.選擇排序
- 6.3.插入排序
- 6.4.快速排序
- 6.5.哈希排序
- 6.6.歸并排序
- 6.7.常見排序算法效率比較
- 6.8.搜索
- 7.樹與樹算法
- 7.1.二叉樹
- 7.2.二叉樹的遍歷
- 初識Django
- 1.小白
- 2.初次嘗試
- 3.管理站點
- 4.視圖
- 5.模板
- django模型
- 1.定義模型
- 2.模型成員
- 3.模型查詢
- 4.自連接
- django視圖
- django模板
- django高級
- django第三方
- django-git