File tree Expand file tree Collapse file tree 4 files changed +49
-3
lines changed Expand file tree Collapse file tree 4 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,25 @@ pub fn (list LinkedList<T>) last() ?T {
40
40
}
41
41
}
42
42
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
+
43
62
// push adds an element to the end of the linked list
44
63
pub fn (mut list LinkedList<T>) push (item T) {
45
64
new_node := & ListNode{
Original file line number Diff line number Diff line change @@ -38,6 +38,17 @@ fn test_last() ? {
38
38
assert false
39
39
}
40
40
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
+
41
52
fn test_push () ? {
42
53
mut list := LinkedList< int > {}
43
54
list.push (1 )
Original file line number Diff line number Diff line change @@ -17,12 +17,17 @@ pub fn (queue Queue<T>) len() int {
17
17
18
18
// peek returns the head of the queue (first element added)
19
19
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 ()
21
21
}
22
22
23
23
// last returns the tail of the queue (last element added)
24
24
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)
26
31
}
27
32
28
33
// push adds an element to the tail of the queue
@@ -32,7 +37,7 @@ pub fn (mut queue Queue<T>) push(item T) {
32
37
33
38
// pop removes the element at the head of the queue and returns it
34
39
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 ()
36
41
}
37
42
38
43
// str returns a string representation of the queue
Original file line number Diff line number Diff line change @@ -38,6 +38,17 @@ fn test_last() ? {
38
38
assert false
39
39
}
40
40
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
+
41
52
fn test_push () ? {
42
53
mut queue := Queue< int > {}
43
54
queue.push (1 )
You can’t perform that action at this time.
0 commit comments