在C中需要一个队列 [英] Need a queue in C

查看:65
本文介绍了在C中需要一个队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个大小为20的FIFO队列,以跟踪一个运行平均值。

一旦队列满了20个值,我想取总和/ 20 =

AVERAGE。然后当一个新值出现时,我想要:


(旧和 - 最旧值+最新值)/ 20 =新平均值


有人知道一种有效的方法吗?队列甚至是最好的方式吗?


谢谢,

I need a FIFO queue of size 20, to keep track of a running average.
Once the queue is full with 20 values, I want to take the sum/20 =
AVERAGE. Then when a new value comes in, I want:

(Old Sum - oldest value + newest value)/20 = New Average

Anybody know an efficient way to implement that? Is a queue even the
best way?

Thanks,

推荐答案

ern写道:
我需要一个大小为20的FIFO队列来跟踪一个运行平均值。
一旦队列满了20个值,我想拿总和/ 20 =
AVERAGE。然后当一个新值出现时,我想要:

(旧和 - 最旧的值+最新值)/ 20 =新的平均值

任何人都知道一种有效的方法来实现它?排队甚至是最好的方式吗?
I need a FIFO queue of size 20, to keep track of a running average.
Once the queue is full with 20 values, I want to take the sum/20 =
AVERAGE. Then when a new value comes in, I want:

(Old Sum - oldest value + newest value)/20 = New Average

Anybody know an efficient way to implement that? Is a queue even the
best way?




这看起来不像C问题,因为问题会是
$ b对于任何编程语言,$ b都是相同的。这听起来更像是数据结构和算法问题。或者也许是家庭作业

问题。


话虽如此,除非你有一个
$ b,否则我不会使用队列$ b队列实现已经完成,你可以使用。原因是,

因为它具有在编译时已知的固定大小,所以你不需要处理具有可以具有的数据结构的复杂性。

中可变数值。


因此,我会使用一个非常简单的数据结构,我会有

a语句在我的程序中看起来像这样:


index =(index + 1)%20;


- Logan



This doesn''t really seem like a C question, since the question would
be the same for just about any programming language. It sounds more
like a data structures and algorithms question. Or maybe a homework
question.

Having said that, I wouldn''t use a queue for this unless you have a
queue implementation already done that you can use. The reason is,
since it has a fixed size known at compile time, you don''t need to
handle the complexity of having a data structure that can have a
variable number of values in it.

Therefore, I would use a very simple data structure and I would have
a statement in my program that looks like this:

index = (index + 1) % 20;

- Logan




Logan Shaw写道:

Logan Shaw wrote:

这看起来不像C问题,因为问题会<几乎任何编程语言都是一样的。听起来更像数据结构和算法问题。或者也许是家庭作业
的问题。


uhhh ...这是一个C问题,因为C不支持std :: queue。

这不是一个功课问题......它实际上更像是一个嵌入式的
系统问题,因为我正在采用浮点电流的值

(模拟 - >数字) 。这些值是波动的,所以我需要实现一些东西来稳定输出。现在,我正在使用

qsort并取中位数。这工作得很好,但是我的应用程序很多都在减慢



因为它在编译时已知固定大小,你不需要处理具有可变数值的数据结构的复杂性。


难道你不是要创建一个没有变量

大小的数据结构来避免这种情况吗?

因此,我会使用一个非常简单的数据结构,我会在我的程序中有一个如下所示的语句:


这个数据结构会是什么样的?

说非常简单的数据结构并没有真正帮助。

index =(index + 1)%20;

This doesn''t really seem like a C question, since the question would
be the same for just about any programming language. It sounds more
like a data structures and algorithms question. Or maybe a homework
question.
uhhh... it kind of IS a C question since C doesn''t support std::queue.
It''s not a homework question... it''s actually more of an embedded
systems question, since I''m taking values of floating point current
(analog -> digital). The values are fluctuating, so I need to
implement something to stabilize the output. Right now, I''m using
qsort and taking the median. This is working great, but it''s slowing
down my application a lot.
since it has a fixed size known at compile time, you don''t need to
handle the complexity of having a data structure that can have a
variable number of values in it.
Wouldn''t you just create a data structure that doesn''t have variable
size to avoid this?
Therefore, I would use a very simple data structure and I would have
a statement in my program that looks like this:
What would that data structure look like? It doesn''t really help to
say "very simple data structure."
index = (index + 1) % 20;




是的...我知道的很多......



yeah... i know that much...


2006-01-13,Logan Shaw< ; LS ********** @ austin.rr.com>写道:
On 2006-01-13, Logan Shaw <ls**********@austin.rr.com> wrote:
ern写道:
我需要一个大小为20的FIFO队列来跟踪一个运行平均值。
一旦队列满了20个值,我想取总和/ 20 =
AVERAGE。然后当一个新值进来时,我想:
I need a FIFO queue of size 20, to keep track of a running average.
Once the queue is full with 20 values, I want to take the sum/20 =
AVERAGE. Then when a new value comes in, I want:


因此,我会使用一个非常简单的数据结构,我会在我的程序中有一个如下所示的语句:

index =(index + 1)%20;


Therefore, I would use a very simple data structure and I would have
a statement in my program that looks like this:

index = (index + 1) % 20;



这看起来类似于循环数组队列。

队列没有必须使用动态分配来实现

是吗?

然后再说一次,也许这是一项任务,而且需要动态分配
。 :-)


-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望它不是太多晚了)

(......它仍然可以......)


This looks similar to a circular array queue.
A queue doesn''t have to be implemented using dynamic allocation
does it?
Then again, maybe this is an assignment and ern needs dynamic
allocation. :-)

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


这篇关于在C中需要一个队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆