我必须仅使用一个队列从上到下按升序排序堆栈
def stack_push(stack, a, size):
if isfull(stack, size):
print("Error, stack is full")
return stack
else:
stack.append(a)
return stack
def stack_pop(stack, size):
if isempty(stack):
return
else:
return stack.pop(0)
def isfull(stack, size):
return len(stack)>=size
def isempty(stack):
return len(stack)==0
s1=[20,20,17,99,8,88,3,10]
temp=[]
s1size=8
我必须编写代码的其余部分,但是我有0个线索如何从这里开始,在不使用插入或任何队列函数的情况下,我该怎么办?另外,如何将堆栈或值从int进行排队索引值进行比较?
def stack_push(stack, a, size):
if isfull(stack, size):
print("Error, stack is full")
return stack
else:
stack.append(a)
return stack
def stack_pop(stack, size):
if isempty(stack):
return
else:
return stack.pop(0)
def isfull(stack, size):
return len(stack)>=size
def isempty(stack):
return len(stack)==0
s1=[20,20,17,99,8,88,3,10]
temp=[]
s1size=8
I have to write the rest of the code but I have 0 clue how to proceed from here, what can I do to solve this without using insert or any queue functions? also how do I compare stacks or values from int to queue index values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个有趣的。即使可能不是您要问的,但只有使用(FIFO)队列对(LIFO)堆栈进行分类的方式!从本质上讲,您只需按顺序排列的顺序保留队列,并在堆栈中对每个元素执行修改后的插入。这是一些可以执行操作的伪代码:
不漂亮!但这应该完成工作。
This is a fun one. Even though it might not be what you are asking, there is a way to sort a (LIFO) stack using only a (FIFO) queue! Essentially, you just keep the queue in a sorted order, and perform a modified insertion for each element as you go through the stack. Here is some pseudo-code that would carry out the operation:
It's not pretty! But it should get the job done.