佇列: 是一種受限的線性資料結構,先進先出,尾端插入頭端出列
順序佇列
陣列實現的佇列: 1.在出隊的時候整個佇列的資料都往頭遷移,這樣的話時間複雜度就是o(n),入隊的時間複雜度為o(1) 2.出隊的時候直接從隊頭彈出乙個資料,然後頭指標往後遷移一步,這**隊操作的時間複雜度是o(1),但是入隊就需要判定下是否佇列滿了,假如滿了但是頭指標在隊頭就擴充,假如頭指標在隊尾就做資料遷移
但是這兩點在python上都體現不出來,因為list是動態陣列
class queue:
def __init__(self):
self.items = list()
self.n = 0
self.head = 0
def enqueue(self,item):
def dequeue(self):
tmp = self.items[self.head]
self.items.remove(self.head)
self.head += 1
return tmp
鏈式佇列
加個tail的尾引用,出隊操作,直接拿head.next作為新的head,入隊則是 把新結點插入到尾結點的next,再把新結點作為尾結點
class node:
def __init__(self,value):
self.value = value
self.next = none
class linkqueue:
def __init__(self):
self.head = none
self.n = 0
self.tail = none
def enqueue(self,value):
new_node = node(value)
if self.head == none :
self.head = new_node
self.tail = self.head
else :
self.tail.next = new_node
self.tail = new_node
self.n += 1
def dequeue(self):
if self.n == 0 : return false
value = self.head.value
self.head = self.head.next
self.n -= 1
return value
還有一種迴圈佇列,可以解決佇列的資料遷移問題,使得用陣列實現的隊列入隊和出隊操作時間複雜度都為o(1) 鏈式棧 鏈式佇列 順序佇列
暑期學習第一天,學習了鏈式棧 鏈式佇列和順序佇列 這三種都是舉一反三的型別 鏈式棧標頭檔案 ifndef stack h define stack h define ok 0 define error 1 結點 typedef struct stacknode stacknode 棧 typedef...
迴圈順序佇列與鏈式佇列實現
佇列是一種先進先出的資料結構,分順序儲存結構和鏈式儲存結構兩種。順序儲存結構中廣泛使用的是迴圈佇列,也是佇列使用中最多的一種。下面將分別實現這兩種佇列的基本操作。includeusing namespace std const int maxsize 100 class queue int isem...
佇列 鏈式佇列
主要是鏈式佇列用單鏈表來實現,說白了還是對單鏈表的應用。為了操作實現方便,這裡採用帶頭結點的單鏈表結構。鍊錶的頭部作為隊首,鍊錶的尾部作為隊尾。一共設定兩個指標,乙個隊首指標和乙個隊尾指標,如圖所示。package 佇列的實現 public inte ce queue package 佇列的實現 p...