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 {
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
4463pub fn (mut list LinkedList<T>) push (item T) {
4564 new_node := & ListNode{
Original file line number Diff line number Diff 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+
4152fn test_push () ? {
4253 mut list := LinkedList< int > {}
4354 list.push (1 )
Original file line number Diff line number Diff 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)
1919pub 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)
2424pub 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
3439pub 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
Original file line number Diff line number Diff 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+
4152fn test_push () ? {
4253 mut queue := Queue< int > {}
4354 queue.push (1 )
You can’t perform that action at this time.
0 commit comments