【问题标题】:enqueue() method adds one element to the queue: how to implement in C++?enqueue() 方法向队列中添加一个元素:如何在 C++ 中实现?
【发布时间】:2009-04-03 01:47:19
【问题描述】:

我很难尝试实现此方法,因为 C++ 中的数组下标从零开始。该方法将一个元素添加到队列中。您可以使用 f(前)和 r(后)指针以及大小为 n 的顺序列表。如果您发现需要额外的变量,请随意。谢谢。

这是我的尝试,但我知道它错了:

void QueueAr::enqueue(const Object& x){
    prov = (r % n) + 1;
    if(prov != f){
        r = prov;
        queueArray[r] = x;
        if(f = -1){
            f = 0
        }
    }else{
        //queue is full
    }
}

如何使用指针?如果我开始他们指向 NULL 我不能使用指针算法。

【问题讨论】:

  • 你可以使用 STL 吗?还是您使用的是普通数组?
  • 在您的示例中,prov、r、f 和 n 是什么?您可以发布您的课程正文吗?
  • 例如,我正在使用分配有 list* Object = new Object[10] 的列表。我需要它来解决这个问题。
  • 如果这是一项编程作业,请考虑在您的课程资料中找到答案,而不是通过在 SO 上获取答案来走捷径。

标签: c++ methods queue


【解决方案1】:

要使用普通数组实现队列,只需循环处理它 - 因此,一旦数组中的空间用完,就回绕到 0。您需要保留前后的记录,因为您笔记。举个例子(其中 X 代表队列中的一个项目):

// Rear is where to enqueue into, Front is where to dequeue from
Empty Array:
| - - - |
Front = -1, Rear = 0 

Enqueue()
| X - - |
Front = 0, Rear = 1

Enqueue()
| X X - |
Front = 0, Rear = 2

Dequeue()
| - X - |
Front = 1, Rear = 2

Enqueue()
| - X X |
Front = 1, Rear = 0 // Looped around

Dequeue()
| - - X |
Front = 2, Rear = 0

Enqueue()
| X - X |
Front = 2, Rear = 1

你只需要使用模运算来环绕。当然,这在大小上是有限的(一旦元素用完,就必须分配更多内存),但这正是处理数组时得到的。

这里有一些代码作为开始(我根本没有检查过):

// Private class variables:
// These should be set in the constructor of your queue class
unsigned int rear = 0; // back of the queue
unsigned int front = -1; // front of the queue
unsigned int numStored = 0;
unsigned int length;
Object* array = new Object[length];

QueueAr::Enqueue(Object& obj)
{
    if (front == rear)
    {
        // Throw an exception: queue is full!
    }
    else
    {
        array[rear] = obj; // Insert the object at the back
        rear++;
        rear = rear % length;
        numStored++;
    }
}
// For kicks, here's the queue code
QueueAr::Dequeue(Object& obj)
{
    if (numStored == 0)
    {
        // Throw an exception: queue is empty!
    }
    front++;
    front = front % length;
    numStored--;
}

【讨论】:

  • 帮助我完成模算术表达式。我现在拥有的那个是错误的,因为最后 r 将获得比数组大小更高的值。
  • 模算术表达式现在显示在我的示例代码中 - 就是说“rear = back % length”的行 - 我认为你也可以做 back %= length。
  • 如果rear = 0,您是否还需要更改前值?因为当队列只有 1 个元素时,需要指向它。
【解决方案2】:

如果您不使用 STL,则可能需要使用链表。要入队,请添加到列表的末尾。要出队,请从列表的开头删除。为了性能和方便,您应该存储列表两端的指针。

【讨论】:

  • 我知道该怎么做,我需要它来处理顺序列表。
【解决方案3】:

如果您受限于特定的数组大小,您可以使用普通的 ol' C 数组轻松实现队列。您需要两个指针/索引:队列的开头和队列的结尾。您还需要数组本身。

您可以通过 (1) 换行或 (2) 在到达末尾时复制数组来防止队列溢出。任何一种方法都相当简单,但包装更容易、更快。使用circular buffer 调用包装数据元素,这对于队列和音频缓冲区很常见。

Queue:  0 1 4 3 6 8 0 0 0 0 0
Start:      ^
End:                ^

Dequeue: return 4 (read, then move start to right)
Queue:  0 1 4 3 6 8 0 0 0 0 0
Start:        ^
End:                ^

Enqueue: 9 (write, then move end to right)
Queue:  0 1 4 3 6 8 9 0 0 0 0
Start:        ^
End:                  ^

Wrapping enqueue: 2 (write, then move to right and wrap to beginning)
Queue:  0 1 4 3 6 8 9 3 4 7 2
Start:        ^
End:    ^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多