Skip to content

Commit db48594

Browse files
authored
datatypes: add an index method on LinkedList and Queue (#13185)
1 parent 2a3a4cf commit db48594

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

vlib/datatypes/linked_list.v

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ pub fn (list LinkedList<T>) last() ?T {
4040
}
4141
}
4242

43+
// index returns the element at the given index of the linked list
44+
pub fn (list LinkedList<T>) index(idx int) ?T {
45+
if list.head == 0 {
46+
return error('Linked list is empty')
47+
} else {
48+
mut node := list.head
49+
mut iterations := 0
50+
for node.next != 0 && iterations < idx {
51+
node = node.next
52+
iterations++
53+
}
54+
if iterations == idx {
55+
return node.data
56+
} else {
57+
return error('Index out of bounds')
58+
}
59+
}
60+
}
61+
4362
// push adds an element to the end of the linked list
4463
pub fn (mut list LinkedList<T>) push(item T) {
4564
new_node := &ListNode{

vlib/datatypes/linked_list_test.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ fn test_last() ? {
3838
assert false
3939
}
4040

41+
fn test_index() ? {
42+
mut list := LinkedList<int>{}
43+
list.push(1)
44+
assert list.index(0) ? == 1
45+
list.push(2)
46+
assert list.index(1) ? == 2
47+
list.pop() ?
48+
list.index(1) or { return }
49+
assert false
50+
}
51+
4152
fn test_push() ? {
4253
mut list := LinkedList<int>{}
4354
list.push(1)

vlib/datatypes/queue.v

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ pub fn (queue Queue<T>) len() int {
1717

1818
// peek returns the head of the queue (first element added)
1919
pub fn (queue Queue<T>) peek() ?T {
20-
return if !queue.is_empty() { queue.elements.first() ? } else { error('Queue is empty') }
20+
return queue.elements.first()
2121
}
2222

2323
// last returns the tail of the queue (last element added)
2424
pub fn (queue Queue<T>) last() ?T {
25-
return if !queue.is_empty() { queue.elements.last() ? } else { error('Queue is empty') }
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)
2631
}
2732

2833
// push adds an element to the tail of the queue
@@ -32,7 +37,7 @@ pub fn (mut queue Queue<T>) push(item T) {
3237

3338
// pop removes the element at the head of the queue and returns it
3439
pub fn (mut queue Queue<T>) pop() ?T {
35-
return if !queue.is_empty() { queue.elements.shift() ? } else { error('Queue is empty') }
40+
return queue.elements.shift()
3641
}
3742

3843
// str returns a string representation of the queue

vlib/datatypes/queue_test.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ fn test_last() ? {
3838
assert false
3939
}
4040

41+
fn test_index() ? {
42+
mut queue := Queue<int>{}
43+
queue.push(1)
44+
assert queue.index(0) ? == 1
45+
queue.push(2)
46+
assert queue.index(1) ? == 2
47+
queue.pop() ?
48+
queue.index(1) or { return }
49+
assert false
50+
}
51+
4152
fn test_push() ? {
4253
mut queue := Queue<int>{}
4354
queue.push(1)

0 commit comments

Comments
 (0)