基于python實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型
模擬棧
- Stack() 創(chuàng)建一個空的新棧。 它不需要參數(shù),并返回一個空棧。
- push(item)將一個新項(xiàng)添加到棧的頂部。它需要 item 做參數(shù)并不返回任何內(nèi)容。
- pop() 從棧中刪除頂部項(xiàng)。它不需要參數(shù)并返回 item 。棧被修改。
- peek() 從棧返回頂部項(xiàng),但不會刪除它。不需要參數(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())
模擬隊(duì)列
- Queue() 創(chuàng)建一個空的新隊(duì)列。 它不需要參數(shù),并返回一個空隊(duì)列。
- enqueue(item) 將新項(xiàng)添加到隊(duì)尾。 它需要 item 作為參數(shù),并不返回任何內(nèi)容。
- dequeue() 從隊(duì)首移除項(xiàng)。它不需要參數(shù)并返回 item。 隊(duì)列被修改。
- isEmpty() 查看隊(duì)列是否為空。它不需要參數(shù),并返回布爾值。
- size() 返回隊(duì)列中的項(xiàng)數(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個孩子圍城一個圈,排列順序孩子們自己指定。第一個孩子手里有一個燙手的山芋,需要在計(jì)時器計(jì)時1秒后將山芋傳遞給下一個孩子,依次類推。規(guī)則是,在計(jì)時器每計(jì)時7秒時,手里有山芋的孩子退出游戲。該游戲直到剩下一個孩子時結(jié)束,最后剩下的孩子獲勝。請使用隊(duì)列實(shí)現(xiàn)該游戲策略,排在第幾個位置最終會獲勝。
準(zhǔn)則:隊(duì)頭孩子的手里永遠(yuǎn)要有山芋。
queue = Queue()
kids = ['A','B','C','D','E','F']
#將六個孩子添加到隊(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ì)列
同同列相比,有兩個頭部和尾部??梢栽陔p端進(jìn)行數(shù)據(jù)的插入和刪除,提供了單數(shù)據(jù)結(jié)構(gòu)中棧和隊(duì)列的特性
- Deque() 創(chuàng)建一個空的新deque。它不需要參數(shù),并返回空的deque。
- addFront(item) 將一個新項(xiàng)添加到deque的首部。它需要item參數(shù)并不返回任何內(nèi)容。
- addRear(item) 將一個新項(xiàng)添加到deque的尾部。它需要item參數(shù)并不返回任何內(nèi)容。
- removeFront() 從deque中刪除首項(xiàng)。它不需要參數(shù)并返回item。deque被修改。
- removeRear() 從deque中刪除尾項(xiàng)。它不需要參數(shù)并返回item。deque被修改。
- isEmpty() 測試deque是否為空。它不需要參數(shù),并返回布爾值。
- size() 返回deque中的項(xiàng)數(shù)。它不需要參數(shù),并返回一個整數(shù)。
案例:回文檢查
回文是一個字符串,讀取首尾相同的字符,例如,radar toot madam。
def isHuiWen(s):
ex = True
q = Dequeue()
# 將字符串的每一個字符添加到雙端隊(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
模擬鏈表
- . is_empty():鏈表是否為空
- . length():鏈表長度
- . travel():遍歷整個鏈表
- . add(item):鏈表頭部添加元素
- . append(item):鏈表尾部添加元素
- . insert(pos, item):指定位置添加元素
- . remove(item):刪除節(jié)點(diǎn)
- . search(item):查找節(jié)點(diǎn)是否存在
結(jié)點(diǎn)對象:
class Node():
def __init__(self,item):
self.item = item
self.next = None
鏈表對象:
class Link():
#構(gòu)建出一個空的鏈表
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的前一個節(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
#刪除的是第一個節(jié)點(diǎn)的情況
if self._head.item == item:
self._head = self._head.next
return
#刪除的是非第一個節(jié)點(diǎn)的情況
while cur:
pre = cur
cur = cur.next
if cur.item == item:
pre.next = cur.next
return
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
- Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)-堆棧和隊(duì)列的操作方法
- Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)-循環(huán)隊(duì)列的操作方法
- python算法與數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)代碼
- Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧隊(duì)列
- 用python實(shí)現(xiàn)各種數(shù)據(jù)結(jié)構(gòu)
相關(guān)文章
Python對ElasticSearch獲取數(shù)據(jù)及操作
這篇文章主要為大家詳細(xì)介紹了Python對ElasticSearch獲取數(shù)據(jù)及操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
python?open函數(shù)中newline參數(shù)實(shí)例詳解
newLine()方法可用于輸出一個換行字符"/n",下面這篇文章主要給大家介紹了關(guān)于python?open函數(shù)中newline參數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Python爬蟲headers處理及網(wǎng)絡(luò)超時問題解決方案
這篇文章主要介紹了Python爬蟲headers處理及網(wǎng)絡(luò)超時問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
YOLOv5車牌識別實(shí)戰(zhàn)教程(二)理論基礎(chǔ)
這篇文章主要介紹了YOLOv5車牌識別實(shí)戰(zhàn)教程(二)理論基礎(chǔ),在這個教程中,我們將一步步教你如何使用YOLOv5進(jìn)行車牌識別,幫助你快速掌握YOLOv5車牌識別技能,需要的朋友可以參考下2023-04-04
使用scrapy實(shí)現(xiàn)爬網(wǎng)站例子和實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(蜘蛛)的步驟
本文分二個示例,第一個是個簡單的爬網(wǎng)站的小例子,第二個例子實(shí)現(xiàn)目是從一個網(wǎng)站的列表頁抓取文章列表,然后存入數(shù)據(jù)庫中,數(shù)據(jù)庫包括文章標(biāo)題、鏈接、時間,大家參考使用吧2014-01-01
Python爬蟲scrapy框架Cookie池(微博Cookie池)的使用
這篇文章主要介紹了Python爬蟲scrapy框架Cookie池(微博Cookie池)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

