0

I'm very new to C++ and am rather confused as to how to make a Queue (our first homework assignment).

For the constructor, we're supposed to accept a length that the Queue should be. The Queue's length must in turn be the closest Fibonacci number greater than the given length if it is not already a Fibonacci number. (e.g.: if given 10 in the constructor, make the length 13).

Right now I'm implementing the Queue as an array. Should I have something like int queueContents[]; in the private part of my header, then set it to the closest Fibonacci number in the constructor?

How would I go about declaring it for use throughout the whole file and set its length in my constructor?

4

2 回答 2

1

您将有一个全局变量来跟踪大小(如果您为了效率需要增加它)。

ArrayQueue(int sz) 
{
   size = getFib(sz);
   queueContents = new int[size];
   //set front and back to initial value   
}

getFib(int sz) {...}

是的,你需要一个全局变量int *queueContents。(指针首选)。

随时问更多问题。我最近也不得不编写一个基本的基于数组的队列,所以(希望)我可以回答你的任何问题。:)

于 2013-10-30T03:34:24.773 回答
0

您可以计算最接近的斐波那契数,然后有一个变量capacity,每次插入队列时,检查这是否超过容量。

于 2013-10-30T02:22:03.877 回答