Skip to content

Commit

Permalink
linked list: Add note for Node struct field Value; change Val to Value (
Browse files Browse the repository at this point in the history
  • Loading branch information
nahuakang authored Apr 27, 2022
1 parent d740b83 commit 7659cc3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
20 changes: 10 additions & 10 deletions exercises/practice/linked-list/.meta/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (

// Node is a node in a linked list.
type Node struct {
Val interface{}
next *Node
prev *Node
Value interface{}
next *Node
prev *Node
}

// NewNode constructs a new Node with the given value & no next/prev links.
func NewNode(v interface{}) *Node {
return &Node{
Val: v,
next: nil,
prev: nil,
Value: v,
next: nil,
prev: nil,
}
}

Expand Down Expand Up @@ -156,13 +156,13 @@ func (ll *List) PopFront() (interface{}, error) {
case ll.head == nil && ll.tail == nil: // empty list
return nil, ErrEmptyList
case ll.head != nil && ll.tail != nil && ll.head.next == nil: // 1 element
v := ll.head.Val
v := ll.head.Value
ll.head = nil
ll.tail = nil

return v, nil
case ll.head != nil && ll.tail != nil && ll.head.next != nil: // >1 element
v := ll.head.Val
v := ll.head.Value
ll.head.next.prev = nil
ll.head = ll.head.next

Expand All @@ -178,13 +178,13 @@ func (ll *List) PopBack() (interface{}, error) {
case ll.head == nil && ll.tail == nil: // empty list
return nil, ErrEmptyList
case ll.head != nil && ll.tail != nil && ll.tail.prev == nil: // 1 element
v := ll.tail.Val
v := ll.tail.Value
ll.head = nil
ll.tail = nil

return v, nil
case ll.head != nil && ll.tail != nil && ll.tail.prev != nil: // >1 element
v := ll.tail.Val
v := ll.tail.Value
ll.tail.prev.next = nil
ll.tail = ll.tail.prev

Expand Down
1 change: 1 addition & 0 deletions exercises/practice/linked-list/linked_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linkedlist

// Define List and Node types here.
// Note: The tests expect Node type to include an exported field with name Value to pass.

func NewList(args ...interface{}) *List {
panic("Please implement the NewList function")
Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/linked-list/linked_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func checkDoublyLinkedList(t *testing.T, ll *List, expected []interface{}) {
// check that length and elements are correct (scan once from begin -> end)
elem, count, idx := ll.First(), 0, 0
for ; elem != nil && idx < len(expected); elem, count, idx = elem.Next(), count+1, idx+1 {
if elem.Val != expected[idx] {
t.Errorf("wrong value from %d-th element, expected= %v, got= %v", idx, expected[idx], elem.Val)
if elem.Value != expected[idx] {
t.Errorf("wrong value from %d-th element, expected= %v, got= %v", idx, expected[idx], elem.Value)
}
}
if !(elem == nil && idx == len(expected)) {
Expand Down Expand Up @@ -98,7 +98,7 @@ func checkDoublyLinkedList(t *testing.T, ll *List, expected []interface{}) {
}
}

// debugString prints the linked list with both node's Val, next & prev pointers.
// debugString prints the linked list with both node's Value, next & prev pointers.
func (ll *List) debugString() string {
buf := bytes.NewBuffer([]byte{'{'})
buf.WriteString(fmt.Sprintf("First()= %p; ", ll.First()))
Expand All @@ -110,7 +110,7 @@ func (ll *List) debugString() string {
if counter > 100 {
panic("Possible infinite loop detected and stopped. Check the .Next() implementation")
}
buf.WriteString(fmt.Sprintf("[Prev()= %p, Val= %p (%v), Next()= %p] <-> ", cur.Prev(), cur, cur.Val, cur.Next()))
buf.WriteString(fmt.Sprintf("[Prev()= %p, Value= %p (%v), Next()= %p] <-> ", cur.Prev(), cur, cur.Value, cur.Next()))
}

buf.WriteString(fmt.Sprintf("; Last()= %p; ", ll.Last()))
Expand Down

0 comments on commit 7659cc3

Please sign in to comment.