我必须仅使用一个队列从上到下按升序排序堆栈

发布于 2025-02-10 06:58:17 字数 540 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2025-02-17 06:58:17

这是一个有趣的。即使可能不是您要问的,但只有使用(FIFO)队列对(LIFO)堆栈进行分类的方式!从本质上讲,您只需按顺序排列的顺序保留队列,并在堆栈中对每个元素执行修改后的插入。这是一些可以执行操作的伪代码:

while (!stk.empty()):
    x = stk.pop() # item to insert
    first = q.peek() # first item in sorted queue

    if (q.peek() < x):
        # condition for new largest item
        q.push(x)
        # x will be new first in sorted queue
        first = x

    else:
        # condition for not-largest item
        q.push(q.pop())
        while (q.peek() > x && q.peek() != first):
            q.push(q.pop())
        q.push(x)

    # now loop so queue is back in it's sorted order
    while (q.peek() != first):
        q.push(q.pop())
 

不漂亮!但这应该完成工作。

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:

while (!stk.empty()):
    x = stk.pop() # item to insert
    first = q.peek() # first item in sorted queue

    if (q.peek() < x):
        # condition for new largest item
        q.push(x)
        # x will be new first in sorted queue
        first = x

    else:
        # condition for not-largest item
        q.push(q.pop())
        while (q.peek() > x && q.peek() != first):
            q.push(q.pop())
        q.push(x)

    # now loop so queue is back in it's sorted order
    while (q.peek() != first):
        q.push(q.pop())
 

It's not pretty! But it should get the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文