理解python中的堆栈和队列
所以我被问到这个问题。考虑具有标准操作集的堆栈和队列类。使用Stack和Queue类,在调用mysteryFunction之前和调用mysteryFunction之后,其中包含哪些项 代码如下:理解python中的堆栈和队列,python,python-3.x,Python,Python 3.x,所以我被问到这个问题。考虑具有标准操作集的堆栈和队列类。使用Stack和Queue类,在调用mysteryFunction之前和调用mysteryFunction之后,其中包含哪些项 代码如下: def mysteryFunction(s, q): q.enqueue('csc148') q.enqueue(True) q.enqueue(q.front()) q.enqueue('abstract data type') for i in range(
def mysteryFunction(s, q):
q.enqueue('csc148')
q.enqueue(True)
q.enqueue(q.front())
q.enqueue('abstract data type')
for i in range(q.size()):
s.push(q.dequeue())
while not s.is_empty():
q.enqueue(s.pop())
if __name__ == '__main__':
s=Stack()
q=Queue()
#About to call mysteryFunction
#What are contents of s and q at this point?
mysteryFunction(s, q)
#mysteryFunction has been called.
#What are contents of s and q at this point?
我对面向对象编程不太了解,因为我不熟悉这个话题。是否有任何链接可以分解堆栈和队列以及它们的作用?通常,堆栈是后进先出,队列是先进先出 在Python中,可以使用collections模块试验堆栈和队列:
>>> from collections import deque
>>> stack = deque()
>>> stack.append(10)
>>> stack.append(20)
>>> stack.append(30)
>>> stack
deque([10, 20, 30])
>>> stack.pop() # LIFO
30
>>> stack.pop()
20
>>>
>>> queue = deque()
>>> queue.append(10)
>>> queue.append(20)
>>> queue.append(30)
>>> queue
deque([10, 20, 30])
>>> queue.popleft() # FIFO
10
>>> queue.popleft()
20
有关详细信息,请参阅以下链接:
从视觉上看,这两个数据结构可以通过以下方式看到: 堆栈: 说明: 这种数据结构有多种变化。然而,简单地说,正如人们在所提供的图像中所看到的,当您添加到这个数据结构时,您将其放在已经存在的数据结构之上,当您删除时,您也将其从顶部移除。你可以把它看作是一堆书,从上往下一本一本地看 队列 说明:
这个特定的数据结构也有一些变化,但简单来说,正如您在提供的图像中所看到的,当您将新元素添加到这个数据结构中时,新元素将进入起始位置,当您从列表中删除它时,它将成为要删除的最后一个元素。你可以把它想象成你在一家商店里排队等待轮到你到柜台付款的许多人的后面。要逐行测试这一点,下面是任务中使用的类(围绕deque的包装器)的实现:
from collections import deque
class Queue(deque):
enqueue = deque.append
dequeue = deque.popleft
def front(self):
return self[-1]
def size(self):
return len(self)
class Stack(deque):
push = deque.append
def is_empty(self):
return not self
堆栈后进先出
队列#先进先出
第一段代码解释了堆栈,我们需要创建一个列表,在推送元素时使用append并填充列表,类似于数组堆栈中的内容。弹出时,从推它的地方弹出末端
class Stack:
def __init__(self):
self.stack = []
def push_element(self,dataval):
self.stack.append(dataval)
return self.stack
def pop_element(self):
if len(self.stack) ==0:
print("Stack is empty")
else:
self.stack.pop()
return self.stack
def peek_element(self):
return self.stack[0]
class Queue:
def __init__(self):
self.stack = []
def push_ele(self,data):
self.stack.append(data)
def pop_ele(self):
self.stack.pop(0)
def display(self):
print(self.stack)
类堆栈:
def __init__(self,n):##constructor
self.no = n ##size of stack
self.Stack = [] ##list for store stack items
self.top = -1
def push(self):##push method
if self.top == self.no - 1 :##check full condition
print("Stack Overflow.....")
else:
n = int(input("enter an element :: "))
self.Stack.append(n) ## in list add stack items use of append method
self.top += 1##increment top by 1
def pop(self):## pop method
if self.top == -1: #check empty condition
print("Stack Underflow....")
else:
self.Stack.pop()## delete item from top of stack using pop method
self.top -= 1 ## decrement top by 1
def peep(self): ##peep method
print(self.top,"\t",self.Stack[-1]) ##display top item
def disp (self): #display method
if self.top == -1:# check empty condition
print("Stack Underflow....")
else:
print("TOP \tELEMENT")
for i in range(self.top,-1,-1): ## print items and top
print(i," \t",self.Stack[i])
n=int(输入(“输入大小:”)#堆栈大小
stk=堆栈(n)##对象并传递n作为大小
while(True):##循环选择作为开关案例
print(" 1: PUSH ")
print(" 2: POP ")
print(" 3: PEEP ")
print(" 4: PRINT ")
print(" 5: EXIT ")
option = int(input("enter your choice :: "))
if option == 1:
stk.push()
elif option == 2:
stk.pop()
elif option == 3:
stk.peep()
elif option == 4:
stk.disp()
elif option == 5:
print("you are exit!!!!!")
break
else:
print("Incorrect option")
感谢您提供的链接。您的任务中的堆栈和队列不是标准库的一部分,但在本例中,
enqueue
与append
相同,dequeue
与popleft
“enqueue”仅表示添加到队列中并“dequeue”表示从FIFO队列中删除。同样,“push”表示添加到堆栈,“pop”表示从后进先出堆栈中删除元素。
def __init__(self,n):##constructor
self.no = n ##size of stack
self.Stack = [] ##list for store stack items
self.top = -1
def push(self):##push method
if self.top == self.no - 1 :##check full condition
print("Stack Overflow.....")
else:
n = int(input("enter an element :: "))
self.Stack.append(n) ## in list add stack items use of append method
self.top += 1##increment top by 1
def pop(self):## pop method
if self.top == -1: #check empty condition
print("Stack Underflow....")
else:
self.Stack.pop()## delete item from top of stack using pop method
self.top -= 1 ## decrement top by 1
def peep(self): ##peep method
print(self.top,"\t",self.Stack[-1]) ##display top item
def disp (self): #display method
if self.top == -1:# check empty condition
print("Stack Underflow....")
else:
print("TOP \tELEMENT")
for i in range(self.top,-1,-1): ## print items and top
print(i," \t",self.Stack[i])
print(" 1: PUSH ")
print(" 2: POP ")
print(" 3: PEEP ")
print(" 4: PRINT ")
print(" 5: EXIT ")
option = int(input("enter your choice :: "))
if option == 1:
stk.push()
elif option == 2:
stk.pop()
elif option == 3:
stk.peep()
elif option == 4:
stk.disp()
elif option == 5:
print("you are exit!!!!!")
break
else:
print("Incorrect option")