From db48594bd4d3ab52ccbb3db9ef64aa7952c714a6 Mon Sep 17 00:00:00 2001 From: Hunam Date: Sun, 16 Jan 2022 18:11:10 +0100 Subject: [PATCH] datatypes: add an index method on LinkedList and Queue (#13185) --- vlib/datatypes/linked_list.v | 19 +++++++++++++++++++ vlib/datatypes/linked_list_test.v | 11 +++++++++++ vlib/datatypes/queue.v | 11 ++++++++--- vlib/datatypes/queue_test.v | 11 +++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/vlib/datatypes/linked_list.v b/vlib/datatypes/linked_list.v index 0bc17c4597..a3b7dc4564 100644 --- a/vlib/datatypes/linked_list.v +++ b/vlib/datatypes/linked_list.v @@ -40,6 +40,25 @@ pub fn (list LinkedList) last() ?T { } } +// index returns the element at the given index of the linked list +pub fn (list LinkedList) index(idx int) ?T { + if list.head == 0 { + return error('Linked list is empty') + } else { + mut node := list.head + mut iterations := 0 + for node.next != 0 && iterations < idx { + node = node.next + iterations++ + } + if iterations == idx { + return node.data + } else { + return error('Index out of bounds') + } + } +} + // push adds an element to the end of the linked list pub fn (mut list LinkedList) push(item T) { new_node := &ListNode{ diff --git a/vlib/datatypes/linked_list_test.v b/vlib/datatypes/linked_list_test.v index 4fee4bf71d..72469dd503 100644 --- a/vlib/datatypes/linked_list_test.v +++ b/vlib/datatypes/linked_list_test.v @@ -38,6 +38,17 @@ fn test_last() ? { assert false } +fn test_index() ? { + mut list := LinkedList{} + list.push(1) + assert list.index(0) ? == 1 + list.push(2) + assert list.index(1) ? == 2 + list.pop() ? + list.index(1) or { return } + assert false +} + fn test_push() ? { mut list := LinkedList{} list.push(1) diff --git a/vlib/datatypes/queue.v b/vlib/datatypes/queue.v index 8aab6ac3b0..757b0af573 100644 --- a/vlib/datatypes/queue.v +++ b/vlib/datatypes/queue.v @@ -17,12 +17,17 @@ pub fn (queue Queue) len() int { // peek returns the head of the queue (first element added) pub fn (queue Queue) peek() ?T { - return if !queue.is_empty() { queue.elements.first() ? } else { error('Queue is empty') } + return queue.elements.first() } // last returns the tail of the queue (last element added) pub fn (queue Queue) last() ?T { - return if !queue.is_empty() { queue.elements.last() ? } else { error('Queue is empty') } + return queue.elements.last() +} + +// index returns the element at the given index of the queue +pub fn (queue Queue) index(idx int) ?T { + return queue.elements.index(idx) } // push adds an element to the tail of the queue @@ -32,7 +37,7 @@ pub fn (mut queue Queue) push(item T) { // pop removes the element at the head of the queue and returns it pub fn (mut queue Queue) pop() ?T { - return if !queue.is_empty() { queue.elements.shift() ? } else { error('Queue is empty') } + return queue.elements.shift() } // str returns a string representation of the queue diff --git a/vlib/datatypes/queue_test.v b/vlib/datatypes/queue_test.v index 2bfc23a8aa..5ffc387f59 100644 --- a/vlib/datatypes/queue_test.v +++ b/vlib/datatypes/queue_test.v @@ -38,6 +38,17 @@ fn test_last() ? { assert false } +fn test_index() ? { + mut queue := Queue{} + queue.push(1) + assert queue.index(0) ? == 1 + queue.push(2) + assert queue.index(1) ? == 2 + queue.pop() ? + queue.index(1) or { return } + assert false +} + fn test_push() ? { mut queue := Queue{} queue.push(1)