您好,登錄后才能下訂單哦!
不懂python如何實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型??其實(shí)想解決這個(gè)問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。
模擬棧
class Stack(): def __init__(self): self.items = [] def push(self,item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return len(self.items) - 1 def isEmpty(self): return self.items == [] def size(self): return len(self.items) s = Stack() s.push(1) s.push(2) s.push(3) print(s.pop()) print(s.pop()) print(s.pop()) print(s.isEmpty())
模擬隊(duì)列
class Queue(): def __init__(self): self.items = [] def enqueue(self,item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def isEmpty(self): return self.items == [] def size(self): return len(self.items) q = Queue() q.enqueue(1) q.enqueue(2) q.enqueue(3) print(q.dequeue()) print(q.dequeue()) print(q.dequeue())
案例:燙手山芋
燙手山芋游戲介紹:6個(gè)孩子圍城一個(gè)圈,排列順序孩子們自己指定。第一個(gè)孩子手里有一個(gè)燙手的山芋,需要在計(jì)時(shí)器計(jì)時(shí)1秒后將山芋傳遞給下一個(gè)孩子,依次類推。規(guī)則是,在計(jì)時(shí)器每計(jì)時(shí)7秒時(shí),手里有山芋的孩子退出游戲。該游戲直到剩下一個(gè)孩子時(shí)結(jié)束,最后剩下的孩子獲勝。請使用隊(duì)列實(shí)現(xiàn)該游戲策略,排在第幾個(gè)位置最終會獲勝。
準(zhǔn)則:隊(duì)頭孩子的手里永遠(yuǎn)要有山芋。
queue = Queue() kids = ['A','B','C','D','E','F'] #將六個(gè)孩子添加到隊(duì)列中,A是隊(duì)頭位置的孩子 for kid in kids: queue.enqueue(kid) while queue.size() > 1: #在7秒之內(nèi)山芋會被傳遞6次 for i in range(6): kid = queue.dequeue() queue.enqueue(kid) queue.dequeue() print('獲勝者為:',queue.dequeue())
模擬雙端隊(duì)列
同同列相比,有兩個(gè)頭部和尾部??梢栽陔p端進(jìn)行數(shù)據(jù)的插入和刪除,提供了單數(shù)據(jù)結(jié)構(gòu)中棧和隊(duì)列的特性
案例:回文檢查
回文是一個(gè)字符串,讀取首尾相同的字符,例如,radar toot madam。
def isHuiWen(s): ex = True q = Dequeue() # 將字符串的每一個(gè)字符添加到雙端隊(duì)列中 for ch in s: q.addFront(ch) for i in range(len(s) // 2): font = q.removeFront() rear = q.removeRear() if font != rear: ex = False break return ex
模擬鏈表
結(jié)點(diǎn)對象:
class Node(): def __init__(self,item): self.item = item self.next = None
鏈表對象:
class Link(): #構(gòu)建出一個(gè)空的鏈表 def __init__(self): self._head = None #永遠(yuǎn)指向鏈表中的頭節(jié)點(diǎn) #想鏈表的頭部插入節(jié)點(diǎn) def add(self,item): node = Node(item) node.next = self._head self._head = node def travel(self): cur = self._head #鏈表為空則輸出‘鏈表為空' if self._head == None: print('鏈表為空!') while cur: print(cur.item) cur = cur.next def isEmpty(self): return self._head == None def length(self): cur = self._head count = 0 while cur: count += 1 cur = cur.next return count def search(self,item): cur = self._head find = False while cur: if cur.item == item: find = True break cur = cur.next return find def append(self,item): node = Node(item) #鏈表為空的情況 if self._head == None: self._head = node return cur = self._head #頭節(jié)點(diǎn) pre = None #cur的前一個(gè)節(jié)點(diǎn) while cur: pre = cur cur = cur.next pre.next = node def insert(self,pos,item): node = Node(item) if pos < 0 or pos > self.length(): print('重新給pos賦值?。?!') return cur = self._head pre = None for i in range(pos): pre = cur cur = cur.next pre.next = node node.next = cur def remove(self,item): cur = self._head pre = None if self._head == None:#鏈表為空 print('鏈表為空,沒有可刪除的節(jié)點(diǎn)!!1') return #刪除的是第一個(gè)節(jié)點(diǎn)的情況 if self._head.item == item: self._head = self._head.next return #刪除的是非第一個(gè)節(jié)點(diǎn)的情況 while cur: pre = cur cur = cur.next if cur.item == item: pre.next = cur.next return
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享python如何實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型?內(nèi)容對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。