v / vlib / datatypes
Raw file | 51 loc (41 sloc) | 1.18 KB | Latest commit hash 7d8c38672
1module datatypes
2
3pub struct Queue[T] {
4mut:
5 elements LinkedList[T]
6}
7
8// is_empty checks if the queue is empty
9pub fn (queue Queue[T]) is_empty() bool {
10 return queue.elements.is_empty()
11}
12
13// len returns the length of the queue
14pub fn (queue Queue[T]) len() int {
15 return queue.elements.len()
16}
17
18// peek returns the head of the queue (first element added)
19pub fn (queue Queue[T]) peek() !T {
20 return queue.elements.first()
21}
22
23// last returns the tail of the queue (last element added)
24pub fn (queue Queue[T]) last() !T {
25 return queue.elements.last()
26}
27
28// index returns the element at the given index of the queue
29pub fn (queue Queue[T]) index(idx int) !T {
30 return queue.elements.index(idx)
31}
32
33// push adds an element to the tail of the queue
34pub fn (mut queue Queue[T]) push(item T) {
35 queue.elements.push(item)
36}
37
38// pop removes the element at the head of the queue and returns it
39pub fn (mut queue Queue[T]) pop() !T {
40 return queue.elements.shift()
41}
42
43// str returns a string representation of the queue
44pub fn (queue Queue[T]) str() string {
45 return queue.elements.str()
46}
47
48// array returns a array representation of the queue
49pub fn (queue Queue[T]) array() []T {
50 return queue.elements.array()
51}