这是一个动态数组数据结构,允许可变大小
队列可以动态增长和缩小,这取决于存储在队列中的元素数量。
队列类型
- 有界队列:可以存储数量有限的元素。
- 无界队列:可以存储无限数量的元素。
下图显示了大小有限的有限队列,如队列大小是5
int q [$:4]
下图显示了无限大小的无限队列,如N
int q [$]
Verilog系统中的队列方法
方法 | 描述 |
---|---|
insert(<index>, <element>) | 在指定的索引/位置插入一个元素。 |
delete(<index>) | 删除指定索引处的元素。 |
delete() | 删除队列的所有元素 |
size() | 返回队列的大小 |
push_front(<element>) | 在队列的前面插入元素 |
pop_front(<element>) | 返回并删除队列的第一个元素 |
push_back(<element>) | 在队列末尾插入元素 |
pop_back(<element>) | 返回并删除队列的最后一个元素 |
shuffle() | 打乱队列中的元素 |
队列示例
输出
Queue Size: 3
Element[0] = 5
Element[1] = 10
Element[2] = 15
After popping the front element:
Element[0] = 10
Element[1] = 15
输出
Size: q = 2
-----------------------
Size: q = 5
q[0] = RED
q[1] = RED
q[2] = BLUE
q[3] = BLUE
q[4] = GREEN
-----------------------
--- Access queue item ---
The second element of q = BLUE
The fourth element of q = GREEN
-----------------------
--- Delete queue item ---
q[0] = RED
q[1] = RED
q[2] = BLUE
q[3] = GREEN
-----------------------
--- Delete complete queue ---
Size after queue deletion: q size = 0
-----------------------
--- push_back methods ---
q[0] = YELLOW
q[1] = WHITE
q[2] = ELEPHANT
-----------------------
--- push_front methods ---
q[0] = RED
q[1] = YELLOW
q[2] = WHITE
q[3] = ELEPHANT
-----------------------
--- pop_back methods ---
animal_q[0] = RED
animal_q[1] = YELLOW
animal_q[2] = WHITE
-----------------------
--- pop_front methods ---
q[0] = YELLOW
q[1] = WHITE
-----------------------
Shuffle()方法示例
让我们看看shuffle方法是如何工作的。
代码
输出
******* Before shuffle *******
num_q[0] = 0
num_q[1] = 1
num_q[2] = 2
num_q[3] = 3
num_q[4] = 4
******** After shuffle *******
num_q[0] = 3
num_q[1] = 1
num_q[2] = 4
num_q[3] = 0
num_q[4] = 2