问题标签 [lifo]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1175 浏览

java - LIFO 堆栈上的序列

我正在准备期中考试,我需要帮助解决这个问题:

假设在 LIFO 堆栈上执行混合的 push 和 pop 操作序列。推动按顺序推动数字 0 到 9;pops 打印出返回值。以下哪个输出序列可能出现?选择所有可能的。

我相信答案是:

我对么?先感谢您!

0 投票
1 回答
60 浏览

collections - Usergrid 集合后进先出

usergrid 集合的默认行为似乎是在执行 GET 时首先检索首先添加的实体。我可以以相反的顺序检索实体,即首先检索最后添加的实体。我知道我可以使用查询 ql=select * order by created desc 来做到这一点,但这是否总是意味着对结果进行排序(具有性能影响)?

例如,如果我有一个 usergrid 博客集合,那么我需要先获取最新的博客,而不必进行排序。

我们如何做到这一点?

0 投票
1 回答
2861 浏览

list - Scheme中LIFO列表的实现

我在 Scheme 中实现 LIFO 列表时遇到了一些麻烦。如果我只想将一个元素压入堆栈,我的代码工作得很好,但我希望能够压入多个元素。这是我的代码:

这只是我的最后一次尝试,我尝试了很多方法,我数不过来。我只是不明白如何从“args”中取出元素并将它们一一添加到堆栈中。“推送”过程根本不起作用,我只是在最后一行得到一个错误(也许我的递归是错误的)。正如我所说,这是我最后一次尝试,我无法理解它。

编辑:

我现在已经尝试实现push!,pop!并将堆栈作为独立过程,将堆栈对象作为参数。流行音乐!并推!很容易,但在推动!我要添加的元素已经嵌套在推送列表中!因此,当我将它发送到 make-stack 过程时,它会以嵌套列表的形式出现。我试图做一个递归过程来解决这个问题:

这只是进入一个循环,我不明白为什么......(递归对我来说是个大问题)

0 投票
2 回答
145 浏览

java - 等待同步块的线程的执行顺序

我有一些线程正在运行,并且都试图进入同步块。

我注意到线程以随机顺序运行(当我调用 Thread.start() 时),没关系

当第一个线程执行并进入同步方法时,它进入睡眠状态..

在此期间,其他线程开始等待同步块被释放。

我的问题是最后一个等待的线程首先获得同步块..

以下是代码。

示例输出为:

运行者:1个橙子

同步占用:1个橙子

运行者: 2 黄色

经营者:4蓝

运行者:0 红色

运行者: 3 绿色

同步发布者:1个橙子

同步占用:3绿

同步发布者:3绿

同步占用:0红色

同步发布者:0 red

同步占用:4蓝

同步发布者:4 blue

同步占用:2 黄色

同步发布者:2 黄色

正如我们看到的线程执行顺序是随机的..没关系.. 1 2 4 0 3

所以 1 得到同步块,在它结束后 3(最后一个)得到它,然后 0 和 4 和 2....

如何使该顺序反转,,,在1之后它必须是2然后4然后0然后3

0 投票
1 回答
1926 浏览

python - 带有 (LIFO) 队列的 Python 多处理

我正在尝试在 Python 中使用多处理来让一个函数在循环中不断被调用,然后访问该函数的最新返回值(通过将值存储在 LIFO 队列中)。

这是主程序的代码片段

这是来自 myFunc 的代码片段

问题是,主循环认为 q 是空的。但是,我检查了 myFunc() 是否将值放入 q(通过在 q.put(x) 之后放置 q.empty() 检查)并且队列不应为空。

我该怎么做才能让主循环看到队列中的值?还是我以低效的方式解决这个问题?(我确实需要 myFunc 和主循环单独运行,因为 myFunc 有点慢,主循环需要继续执行它的任务)

0 投票
1 回答
772 浏览

c# - LIFO 堆栈(Windows 应用程序)

我需要用 windows 窗体做一个练习,但我是 c# 的初学者程序员,我需要帮助..

首先...我需要在 Windows 窗体上列出这些数字:-20、45、-10、20、1 但我不知道我是否只需要使用工具箱的标签并更改值来做到这一点。

第二......我需要创建按钮,当我点击时,它只会消失负值(-20,-10)

三...我需要另一个在 LIFO 中订购数字的按钮

有人可以帮我上传示例或程序吗?我不知道该怎么做

0 投票
1 回答
863 浏览

bash - How do I create a Stack or LIFO for GNU Parallel in Bash

While my original problem was solved in a different manner (see comment thread under this question, as well as the edits to this question), I was able to create a stack/LIFO for GNU Parallel in Bash. So I will edited my background/question to reflect a situation where it could be needed.

Background

I am using GNU Parallel to process files with a Bash script. As the files are processed, more files are created and new commands need to be added to parallel's list. I am not able to give parallel a complete list of commands, as information is generated as the initial files are processed.

I need a way to add the lines to parallel's list while it is running.

Parallel will also need to wait for a new line if nothing is in the queue and exit once the queue is finished.

Solution

First I created a fifo:

Next I created a bash file that cat's the file and pipes the output to parallel, which checks for the end_of_file line. (I wrote this with help from the accepted answer as well as from here)

Then I write to the pipe with this command, adding lines to parallel's queue:

With this setup, all new commands are added to the queue. Once the queue is full parallel will begin processing it. This means that if you have slots for 32 jobs (32 processors), then you will need to add 32 jobs in order to start the queue.

If parallel is occupying all of its processors, it will put the job on hold until a processor becomes available.

By using the --ungroup argument, parallel will process/output jobs as they are added to the queue once the queue is full.

Without the --ungroup argument, parallel waits until a new slot is needed to complete a job. From the accepted answer:

Output from the running or completed jobs are held back and will only be printed when JobSlots more jobs has been started (unless you use --ungroup or -u, in which case the output from the jobs are printed immediately). E.g. if you have 10 jobslots then the output from the first completed job will only be printed when job 11 has started, and the output of second completed job will only be printed when job 12 has started.

0 投票
3 回答
2927 浏览

python - 如何在 python 中为 multiprocessing.Queue 实现 LIFO?

我了解队列和堆栈之间的区别。但是,如果我生成多个进程并在它们之间发送消息,multiprocessing.Queue我如何首先访问队列中的最新元素?

0 投票
2 回答
47 浏览

python - 如何对我的代码进行编程,以便它在 python 中为 LIFO 结构显示从上到下的堆栈顺序

我已经让代码工作了,但是我已经在 python 中编写了实现堆栈的代码,它在 LIFO 之后推送和弹出,但是当您查看列表时,它会将其打印为:

显示底部的最后一项是 3,我怎样才能让它像正确的堆栈一样在顶部显示最后一项?

我的代码如下:

0 投票
3 回答
3707 浏览

java - 无法控制共享首选项中字符串集的顺序

这是我的第一个 stackoverflow 问题。我在这方面做了很多谷歌搜索。在 Hashsets、Treesets、LinkedHashSets、Collections、Stacks(不推荐使用 Stack 类?)...我意识到我可以只使用 SQLite,但我暂时试图避免这种情况。

我正在开发 Android Studio 中的应用程序。该应用程序与人打交道,列出他们并以不同的方式联系他们。App 用户可以维护和控制三种类型的列表:最近联系、阻止和收藏。这些列表保存为共享首选项中的字符串集,因此它们在应用程序关闭和重新打开时仍然存在。当使用在线数据库填充列表时,各个字符串充当主键。我最关心的是“最近联系”列表,因为这个列表的顺序很重要。

问题是,据我了解,当我声明一个字符串集时,如下所示:

据我了解,我只能在右侧使用 HashSet、LinkedHashSet 或 TreeSet。

因为我使用了 LinkedHashset,所以我期望当我重新打开应用程序并将数据从 sharedpreferences 中拉回时,字符串将从最近添加的(LIFO / stack)开始被拉出,因此当我填充列表视图时,他们将与列表顶部的“最近联系”的人一起显示,依此类推……或者至少我期望有某种可预测的顺序/行为,我可以使用。

所以.......我已经为我的共享首选项 I/O 类附加了我的代码。

在我的主应用程序中,我执行以下操作:

此时,代码

在 SharedPrefUTIL Logs 类的 commit() 方法中:

" [derp, nerp, gerp, herp] 致力于recentArray "

Bur 在关闭并重新打开应用程序后,如果我们执行:

结果似乎是随机顺序。<<<<这是我的问题。

非常感谢任何帮助。