1 | module datatypes |
2 | |
3 | pub struct Queue[T] { |
4 | mut: |
5 | elements LinkedList[T] |
6 | } |
7 | |
8 | // is_empty checks if the queue is empty |
9 | pub fn (queue Queue[T]) is_empty() bool { |
10 | return queue.elements.is_empty() |
11 | } |
12 | |
13 | // len returns the length of the queue |
14 | pub fn (queue Queue[T]) len() int { |
15 | return queue.elements.len() |
16 | } |
17 | |
18 | // peek returns the head of the queue (first element added) |
19 | pub fn (queue Queue[T]) peek() !T { |
20 | return queue.elements.first() |
21 | } |
22 | |
23 | // last returns the tail of the queue (last element added) |
24 | pub 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 |
29 | pub 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 |
34 | pub 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 |
39 | pub fn (mut queue Queue[T]) pop() !T { |
40 | return queue.elements.shift() |
41 | } |
42 | |
43 | // str returns a string representation of the queue |
44 | pub fn (queue Queue[T]) str() string { |
45 | return queue.elements.str() |
46 | } |
47 | |
48 | // array returns a array representation of the queue |
49 | pub fn (queue Queue[T]) array() []T { |
50 | return queue.elements.array() |
51 | } |