数据结构:线性数据结构(1)-栈(栈,队列,deques, 列表)
棧,隊(duì)列,deques, 列表是一類容器,他們數(shù)據(jù)項(xiàng)之間的順序由添加或刪除的順序決定,一旦一個(gè)數(shù)據(jù)項(xiàng)被添加,它相對于前后元素一直保持該位置不變。注入此類的數(shù)據(jù)結(jié)構(gòu)稱為線性數(shù)據(jù)結(jié)構(gòu)。
棧
棧(棧,隊(duì)列,deques, 列表)是一個(gè)項(xiàng)的有序集合:棧的底部很重要,因?yàn)樵跅V锌拷撞康捻?xiàng)是存儲(chǔ)時(shí)間最長的。最近添加的項(xiàng)是最先會(huì)被移除的。這種排序原則有時(shí)被稱為 LIFO,后進(jìn)先出。
1.棧的抽象數(shù)據(jù)類型
棧被構(gòu)造為項(xiàng)的有序集合,其中項(xiàng)被添加和從末端一處的位置稱為頂部,棧是有序的LIFO。棧操作如下:
- Stack()創(chuàng)建一個(gè)空的新棧。它不需要參數(shù),并返回一個(gè)空棧。
- push(item)將一個(gè)新項(xiàng)添加到棧的頂部。它需要item做參數(shù)并不返回任何內(nèi)容。
- pop()從棧中刪除頂部項(xiàng)。它不需要參數(shù)并返回item。棧被修改。
- peek()從棧返回頂部項(xiàng),但不會(huì)刪除它。不需要參數(shù),不修改棧。
- isEmpty()測試棧是否為空。不需要參數(shù),并返回boolean值。
- size()返回棧中的item數(shù)量。不需要參數(shù),并返回一個(gè)整數(shù)。
2.python實(shí)現(xiàn)棧
#棧 class Stack(object):def __init__(self, ):self.items = list() def isEmpty(self):return self.items == []def size(self):return len(self.items)def push(self, item):self.items.append( item)def pop(self):return self.items.pop()def peek(self):return self.items[-1]3.實(shí)例應(yīng)用?
3.1括號(hào)匹配
# 判斷括號(hào)匹配 def parChecker(symbolString):s = Stack()balance = Trueindex = 0while index < len(symbolString) and balance:symbol = symbolString[index]if symbol in '([{':s.push(symbol)else:if s.isEmpty():balance = Falsebreakelse:top = s.pop()if not matches(top, symbol):balance = Falseindex += 1if balance and s.isEmpty():return Trueelse:return Falsedef matches(open,close):opens = "([{"closers = ")]}"return opens.index(open) == closers.index(close)print(parChecker('[(()){}]')) print(parChecker('')) print(parChecker('()()))')) print(parChecker('[{}]('))?3.2十進(jìn)制轉(zhuǎn)二進(jìn)制
#十進(jìn)制轉(zhuǎn)二進(jìn)制 def dec2bin(num):s = Stack()while num > 0:r = num % 2s.push(r)num //= 2binstring = ''while not s.isEmpty():binstring = binstring + str(s.pop())return binstring dec2bin(233)3.3中綴前綴和后綴表達(dá)式?
中綴轉(zhuǎn)換為后綴的算法:
首先假設(shè)中綴表達(dá)式是一個(gè)由空格分隔的標(biāo)記字符串。 操作符標(biāo)記是*,/,+和?-?,以及左右括號(hào)。操作數(shù)是單字符 A,B,C 等。 以下步驟將后綴順序生成一個(gè)字符串。
- 如果標(biāo)記是操作數(shù),將其附加到輸出列表的末尾。
- 如果標(biāo)記是左括號(hào),將其壓到 opstack 上。
- 如果標(biāo)記是右括號(hào),則彈出 opstack,直到刪除相應(yīng)的左括號(hào)。將每個(gè)運(yùn)算符附加到輸出列表的末尾。
- 如果標(biāo)記是運(yùn)算符,*,/,+或?-?,將其壓入 opstack。但是,首先刪除已經(jīng)在 opstack 中具有更高或相等優(yōu)先級的任何運(yùn)算符,并將它們加到輸出列表中。
下圖展示了對表達(dá)式?A * B + C * D?的轉(zhuǎn)換算法。
#中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式 def infix2Posfix(exp):prec = {}prec['('] = 1prec['+'] = 2prec['-'] = 2prec['*'] = 3prec['/'] = 3s = Stack()postfixList = []tokenList = exp.split()print(tokenList)for token in tokenList: # print(token)if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":postfixList.append(token)elif token == '(':s.push(token)elif token == ')':top = s.pop()while top != '(':postfixList.append(top)top = s.pop() # s.pop()else: # top = s.peek()while (not s.isEmpty() and prec[token] <= prec[s.peek()] ):postfixList.append(s.pop()) # print(s.size())s.push(token)while not s.isEmpty():postfixList.append(s.pop())return ' '.join(postfixList)s = 'A * B + C * D' s1 = 'A + B * C + D' s2 = '( A + B ) * ( C + D )' print(infix2Posfix(s)) print(infix2Posfix(s1)) print(infix2Posfix(s2))?后綴表達(dá)式求值:
假設(shè)后綴表達(dá)式是一個(gè)由空格分隔的標(biāo)記字符串。 運(yùn)算符為*,/,+和?-?,操作數(shù)假定為單個(gè)整數(shù)值。 輸出將是一個(gè)整數(shù)結(jié)果。
- 如果標(biāo)記是操作數(shù),將其從字符串轉(zhuǎn)換為整數(shù),并將值壓到operandStack。
- 如果標(biāo)記是運(yùn)算符*,/,+或-,它將需要兩個(gè)操作數(shù)。彈出operandStack 兩次。 第一個(gè)彈出的是第二個(gè)操作數(shù),第二個(gè)彈出的是第一個(gè)操作數(shù)。執(zhí)行算術(shù)運(yùn)算后,將結(jié)果壓到操作數(shù)棧中。
其他線性數(shù)據(jù)結(jié)構(gòu):
隊(duì)列:https://blog.csdn.net/qq_18888869/article/details/88134592
列表(鏈表):https://blog.csdn.net/qq_18888869/article/details/88138785
deque:https://blog.csdn.net/qq_18888869/article/details/88137237
github代碼:https://github.com/makang101/python-data-structure
?
參考:
problem-solving-with-algorithms-and-data-structure-using-python 中文版
數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴(yán)蔚敏
總結(jié)
以上是生活随笔為你收集整理的数据结构:线性数据结构(1)-栈(栈,队列,deques, 列表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法:树与二叉树python实
- 下一篇: 数据结构:线性数据结构(2)-队列(栈,