欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于python實現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型

 更新時間:2020年06月12日 10:40:58   作者:Hedger_Lee  
這篇文章主要介紹了基于python實現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

模擬棧

  • Stack() 創(chuàng)建一個空的新棧。 它不需要參數(shù),并返回一個空棧。
  • push(item)將一個新項添加到棧的頂部。它需要 item 做參數(shù)并不返回任何內(nèi)容。
  • pop() 從棧中刪除頂部項。它不需要參數(shù)并返回 item 。棧被修改。
  • peek() 從棧返回頂部項,但不會刪除它。不需要參數(shù)。 不修改棧。
  • isEmpty() 測試棧是否為空。不需要參數(shù),并返回布爾值。
  • size() 返回棧中的 item 數(shù)量。不需要參數(shù),并返回一個整數(shù)。
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())

模擬隊列

  • Queue() 創(chuàng)建一個空的新隊列。 它不需要參數(shù),并返回一個空隊列。
  • enqueue(item) 將新項添加到隊尾。 它需要 item 作為參數(shù),并不返回任何內(nèi)容。
  • dequeue() 從隊首移除項。它不需要參數(shù)并返回 item。 隊列被修改。
  • isEmpty() 查看隊列是否為空。它不需要參數(shù),并返回布爾值。
  • size() 返回隊列中的項數(shù)。它不需要參數(shù),并返回一個整數(shù)。
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個孩子圍城一個圈,排列順序孩子們自己指定。第一個孩子手里有一個燙手的山芋,需要在計時器計時1秒后將山芋傳遞給下一個孩子,依次類推。規(guī)則是,在計時器每計時7秒時,手里有山芋的孩子退出游戲。該游戲直到剩下一個孩子時結(jié)束,最后剩下的孩子獲勝。請使用隊列實現(xiàn)該游戲策略,排在第幾個位置最終會獲勝。

準(zhǔn)則:隊頭孩子的手里永遠要有山芋。

queue = Queue()
kids = ['A','B','C','D','E','F']
#將六個孩子添加到隊列中,A是隊頭位置的孩子
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())

模擬雙端隊列

同同列相比,有兩個頭部和尾部。可以在雙端進行數(shù)據(jù)的插入和刪除,提供了單數(shù)據(jù)結(jié)構(gòu)中棧和隊列的特性

  • Deque() 創(chuàng)建一個空的新deque。它不需要參數(shù),并返回空的deque。
  • addFront(item) 將一個新項添加到deque的首部。它需要item參數(shù)并不返回任何內(nèi)容。
  • addRear(item) 將一個新項添加到deque的尾部。它需要item參數(shù)并不返回任何內(nèi)容。
  • removeFront() 從deque中刪除首項。它不需要參數(shù)并返回item。deque被修改。
  • removeRear() 從deque中刪除尾項。它不需要參數(shù)并返回item。deque被修改。
  • isEmpty() 測試deque是否為空。它不需要參數(shù),并返回布爾值。
  • size() 返回deque中的項數(shù)。它不需要參數(shù),并返回一個整數(shù)。

案例:回文檢查

回文是一個字符串,讀取首尾相同的字符,例如,radar toot madam。

def isHuiWen(s):
  ex = True
  q = Dequeue()
  # 將字符串的每一個字符添加到雙端隊列中
  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

模擬鏈表

  • . is_empty():鏈表是否為空
  • . length():鏈表長度
  • . travel():遍歷整個鏈表
  • . add(item):鏈表頭部添加元素
  • . append(item):鏈表尾部添加元素
  • . insert(pos, item):指定位置添加元素
  • . remove(item):刪除節(jié)點
  • . search(item):查找節(jié)點是否存在

結(jié)點對象:

class Node():
  def __init__(self,item):
    self.item = item
    self.next = None

鏈表對象:

class Link():
  #構(gòu)建出一個空的鏈表
  def __init__(self):
    self._head = None #永遠指向鏈表中的頭節(jié)點
    #想鏈表的頭部插入節(jié)點
  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é)點
    pre = None #cur的前一個節(jié)點
    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é)點!!1')
      return
    #刪除的是第一個節(jié)點的情況
    if self._head.item == item:
      self._head = self._head.next
      return

    #刪除的是非第一個節(jié)點的情況
    while cur:
      pre = cur
      cur = cur.next
      if cur.item == item:
        pre.next = cur.next
        return

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python對ElasticSearch獲取數(shù)據(jù)及操作

    Python對ElasticSearch獲取數(shù)據(jù)及操作

    這篇文章主要為大家詳細(xì)介紹了Python對ElasticSearch獲取數(shù)據(jù)及操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • python中關(guān)于tqdm的用法

    python中關(guān)于tqdm的用法

    這篇文章主要介紹了python中關(guān)于tqdm的用法及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 詳解Python locals()的陷阱

    詳解Python locals()的陷阱

    這篇文章主要介紹了詳解Python locals()的陷阱,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • python?open函數(shù)中newline參數(shù)實例詳解

    python?open函數(shù)中newline參數(shù)實例詳解

    newLine()方法可用于輸出一個換行字符"/n",下面這篇文章主要給大家介紹了關(guān)于python?open函數(shù)中newline參數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Python爬蟲headers處理及網(wǎng)絡(luò)超時問題解決方案

    Python爬蟲headers處理及網(wǎng)絡(luò)超時問題解決方案

    這篇文章主要介紹了Python爬蟲headers處理及網(wǎng)絡(luò)超時問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • YOLOv5車牌識別實戰(zhàn)教程(二)理論基礎(chǔ)

    YOLOv5車牌識別實戰(zhàn)教程(二)理論基礎(chǔ)

    這篇文章主要介紹了YOLOv5車牌識別實戰(zhàn)教程(二)理論基礎(chǔ),在這個教程中,我們將一步步教你如何使用YOLOv5進行車牌識別,幫助你快速掌握YOLOv5車牌識別技能,需要的朋友可以參考下
    2023-04-04
  • python matplotlib庫直方圖繪制詳解

    python matplotlib庫直方圖繪制詳解

    這篇文章主要介紹了python matplotlib庫直方圖繪制詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • python加載自定義詞典實例

    python加載自定義詞典實例

    今天小編就為大家分享一篇python加載自定義詞典實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 使用scrapy實現(xiàn)爬網(wǎng)站例子和實現(xiàn)網(wǎng)絡(luò)爬蟲(蜘蛛)的步驟

    使用scrapy實現(xiàn)爬網(wǎng)站例子和實現(xiàn)網(wǎng)絡(luò)爬蟲(蜘蛛)的步驟

    本文分二個示例,第一個是個簡單的爬網(wǎng)站的小例子,第二個例子實現(xiàn)目是從一個網(wǎng)站的列表頁抓取文章列表,然后存入數(shù)據(jù)庫中,數(shù)據(jù)庫包括文章標(biāo)題、鏈接、時間,大家參考使用吧
    2014-01-01
  • Python爬蟲scrapy框架Cookie池(微博Cookie池)的使用

    Python爬蟲scrapy框架Cookie池(微博Cookie池)的使用

    這篇文章主要介紹了Python爬蟲scrapy框架Cookie池(微博Cookie池)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論