2021-12-26 15:01:36 +01:00
|
|
|
module datatypes
|
2021-12-23 17:57:15 +01:00
|
|
|
|
|
|
|
pub struct Queue<T> {
|
|
|
|
mut:
|
|
|
|
elements LinkedList<T>
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_empty checks if the queue is empty
|
|
|
|
pub fn (queue Queue<T>) is_empty() bool {
|
|
|
|
return queue.elements.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
// len returns the length of the queue
|
|
|
|
pub fn (queue Queue<T>) len() int {
|
|
|
|
return queue.elements.len()
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:01:47 +01:00
|
|
|
// peek returns the head of the queue (first element added)
|
2021-12-23 17:57:15 +01:00
|
|
|
pub fn (queue Queue<T>) peek() ?T {
|
2022-01-16 18:11:10 +01:00
|
|
|
return queue.elements.first()
|
2021-12-23 17:57:15 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 07:01:47 +01:00
|
|
|
// last returns the tail of the queue (last element added)
|
|
|
|
pub fn (queue Queue<T>) last() ?T {
|
2022-01-16 18:11:10 +01:00
|
|
|
return queue.elements.last()
|
|
|
|
}
|
|
|
|
|
|
|
|
// index returns the element at the given index of the queue
|
|
|
|
pub fn (queue Queue<T>) index(idx int) ?T {
|
|
|
|
return queue.elements.index(idx)
|
2021-12-29 07:01:47 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:57:15 +01:00
|
|
|
// push adds an element to the tail of the queue
|
|
|
|
pub fn (mut queue Queue<T>) push(item T) {
|
|
|
|
queue.elements.push(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pop removes the element at the head of the queue and returns it
|
|
|
|
pub fn (mut queue Queue<T>) pop() ?T {
|
2022-01-16 18:11:10 +01:00
|
|
|
return queue.elements.shift()
|
2021-12-23 17:57:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// str returns a string representation of the queue
|
|
|
|
pub fn (queue Queue<T>) str() string {
|
|
|
|
return queue.elements.str()
|
|
|
|
}
|