資料結構(3)佇列及棧實現字尾表示式 01 28

2021-09-10 07:36:41 字數 3921 閱讀 2280

#普通佇列

class myquene(object):

def __init__(self,list=):

self.__list = list

def size(self):

return len(self.__list)

def __delete__(self):

if self.size == 0:

print("空佇列不能刪除")

else:

temp_delete = self.__list[0]

del self.__list[0]

return temp_delete

def insert(self,data):

def peek(self):

if self.size == 0:

print("空佇列不能刪除")

else:

return self.__list[0]

#優先順序佇列

class mypquene(object):

def __init__(self,list=):

self.__list = list

def size(self):

return len(self.__list)

def __delete__(self):

if self.size == 0:

print("空佇列不能刪除")

else:

temp_delete = self.__list[0]

del self.__list[0]

return temp_delete

def insert(self,data):

if self.size == 0:

else:

temp = -1

for i in range(len(self.__list)):

if self.__list[1] > data:

temp = i

break

if temp == -1:

else:

self.__list.insert(temp,data)

def peek(self):

if self.size == 0:

print("空佇列不能檢視")

else:

return self.__list[0]

#迴圈佇列

class cycle():

def __init__(self,list=,first=none,last=none):

self.__list = list

self.__last = last

self.__first = first

def size(self):

length = 0

if self.__last > self.__first:

length = self.__last - self.__first + 1

elif self.__last == self.__first and self.__last != none:

length = 1

elif self.__last == self.__first and self.__last == none:

length = 0

else:

length = len(self.__list) - (self.__first - self.__last - 1)

return length

def insert(self,data):

if self.size == 0:

self.__first = 0

self.__last = 0

else:

if self.size() == len(self.__list):

self.insert(self.__last+1,data)

self.__last = self.__last+1

else:

if self.__last+1 == len(self.__list):

self.__list[0] = data

self.__last = 0

else:

self.__list[self.__last+1] = data

self.__last = self.__last+1

def delete(self):

if self.size == 0:

print("空佇列,無法刪除")

elif self.size() == 1:

temp = self.__first

self.__first = none

self.__last = none

return temp

else:

temp = self.__list[self.__first]

if self.__first+1 == len(self.__list):

self.__first = 0

else:

self.__first = self.__first+1

return temp

def peek(self):

if self.size == 0:

print("空佇列,無法檢視")

else:

return self.__list[self.__first]

步驟:

1,中綴表示式轉字尾表示式

1)遇到運算元直接拼到字串

2)遇到操作符

a,遇到左括號

b,遇到右括號

c,遇到加減

d,遇到乘除

2,計算字尾表示式

#中綴表示式轉字尾表示式

def middle2after(s=""):

rule =

str = ""

list =

for item in s:

if item.isalnum():

str += item

else:

if item == "(":

elif item == ")":

while list[-1] != "(":

str += list.pop()

list.pop()

elif item in "+-":

if len(list) == 0:

else:

while len(list) != 0 and list[-1] != "(":

str += list.pop()

elif item in "*/":

else:

print("表示式有問題")

while len(list) != 0:

str += list.pop()

return str

#字尾表示式

def cal(s=""):

stack =

for item in s:

if item.isalnum():

else:

a = float(stack.pop())

b = float(stack.pop())

if item == "-":

c = b - a

elif item == "+":

c = b + a

elif item == "*":

c = b * a

elif item == "/":

c = b / a

else:

print("字尾表示式有問題")

return stack.pop()

資料結構3 佇列與棧專題

迴圈佇列實現類 package ch03 列隊類 public class mycyclequeue 帶引數的構造方法,引數為陣列的大小 public mycyclequeue int maxsize 新增資料,從隊尾插入 public void insert long value arr end ...

資料結構5 佇列

5.1簡介 佇列 queue 和堆疊一樣是一種有序鍊錶,屬於抽象資料型別。不同在於是先進先出 first in,first out,fifo 堆疊只需要乙個top指標指向堆疊頂端即可,但是佇列必須使用front和rear兩個指標分別指向佇列的前端和尾端。基本操作 1 create 建立空佇列 2 a...

資料結構(二) 佇列

一 佇列定義 佇列是限定在一端進行插入,另一端進行刪除特殊線性表。二 佇列基本操作 入隊出隊 三 佇列例題 1.例1 舞伴配對問題 分析 這一題是一道經典的取模運算,每一次將編號往前加一位,到達n就取模。include include include include int main return ...