## 雙向鏈表
一種更復雜的鏈表是“雙向鏈表”或“雙面鏈表”。每個節點有兩個鏈接:一個指向前一個節點,當此節點為第一個節點時,指向空值;而另一個指向下一個節點,當此節點為最后一個節點時,指向空值。

### 操作
* is_empty() 鏈表是否為空
* length() 鏈表長度
* travel() 遍歷鏈表
* add(item) 鏈表頭部添加
* append(item) 鏈表尾部添加
* insert(pos, item) 指定位置添加
* remove(item) 刪除節點
* search(item) 查找節點是否存在
### 實現
~~~
class Node(object):
"""雙向鏈表節點"""
def __init__(self, item):
self.item = item
self.next = None
self.prev = None
class DLinkList(object):
"""雙向鏈表"""
def __init__(self):
self._head = None
def is_empty(self):
"""判斷鏈表是否為空"""
return self._head == None
def length(self):
"""返回鏈表的長度"""
cur = self._head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def travel(self):
"""遍歷鏈表"""
cur = self._head
while cur != None:
print cur.item,
cur = cur.next
print ""
def add(self, item):
"""頭部插入元素"""
node = Node(item)
if self.is_empty():
# 如果是空鏈表,將_head指向node
self._head = node
else:
# 將node的next指向_head的頭節點
node.next = self._head
# 將_head的頭節點的prev指向node
self._head.prev = node
# 將_head 指向node
self._head = node
def append(self, item):
"""尾部插入元素"""
node = Node(item)
if self.is_empty():
# 如果是空鏈表,將_head指向node
self._head = node
else:
# 移動到鏈表尾部
cur = self._head
while cur.next != None:
cur = cur.next
# 將尾節點cur的next指向node
cur.next = node
# 將node的prev指向cur
node.prev = cur
def search(self, item):
"""查找元素是否存在"""
cur = self._head
while cur != None:
if cur.item == item:
return True
cur = cur.next
return False
~~~
**指定位置插入節點**

~~~
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的prev指向cur
node.prev = cur
# 將node的next指向cur的下一個節點
node.next = cur.next
# 將cur的下一個節點的prev指向node
cur.next.prev = node
# 將cur的next指向node
cur.next = node
~~~
**刪除元素**

~~~
def remove(self, item):
"""刪除元素"""
if self.is_empty():
return
else:
cur = self._head
if cur.item == item:
# 如果首節點的元素即是要刪除的元素
if cur.next == None:
# 如果鏈表只有這一個節點
self._head = None
else:
# 將第二個節點的prev設置為None
cur.next.prev = None
# 將_head指向第二個節點
self._head = cur.next
return
while cur != None:
if cur.item == item:
# 將cur的前一個節點的next指向cur的后一個節點
cur.prev.next = cur.next
# 將cur的后一個節點的prev指向cur的前一個節點
cur.next.prev = cur.prev
break
cur = cur.next
~~~
**測試**
~~~
if __name__ == "__main__":
ll = DLinkList()
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(4)
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