func initializePalindromeTests() []palindromeTestPair { l1 := ds.LinkedList{} // palindrome l2 := ds.LinkedList{} // non-palindrome // create a palindrome list // move until half of the list to create a palindrome list for i := 0; i < 5; i++ { n := ds.Node{} n.SetValue(i) l1.Add(&n) } // move from half of the list to create a palindrome list for i := 5; i >= 0; i-- { n := ds.Node{} n.SetValue(i) l1.Add(&n) } // create a non-palindrome list for i := 0; i < 10; i++ { n := ds.Node{} n.SetValue(i) l2.Add(&n) } // we can expand test cases var palindromeTests = []palindromeTestPair{ {l1, true}, {l2, false}, } return palindromeTests }
// partition given list into two parts due to the given value // and then merge them together. func PartitionList(l datastructures.LinkedList, value int) datastructures.LinkedList { beforeValue := datastructures.LinkedList{} afterValue := datastructures.LinkedList{} head := l.Head() for head != nil { n := datastructures.Node{} n.SetValue(head.GetValue()) if head.GetValue() < value { beforeValue.Add(&n) } else { afterValue.Add(&n) } head = head.Next() } beforeValue.SetTail(afterValue.Head()) return beforeValue }
func initializeForwardList() []sumLinkedListForwardTestPair { l1 := datastructures.LinkedList{} l2 := datastructures.LinkedList{} // l1 : 1->2->3->4 = 1234 // l2 : 1->2->3 = 123 // summed value = 1357 value := 1357 // Create l1 : 1->2->3->4 = 1234 for i := 1; i < 5; i++ { n := datastructures.Node{} n.SetValue(i) l1.Add(&n) } // Create l2 : 1->2->3 = 123 for i := 1; i < 4; i++ { n := datastructures.Node{} n.SetValue(i) l2.Add(&n) } // we can expand test cases var forwardTests = []sumLinkedListForwardTestPair{ {l1, l2, value}, } return forwardTests }
func initializeReverseList() []sumLinkedListReverseTestPair { l1 := datastructures.LinkedList{} l2 := datastructures.LinkedList{} // l2 : 1->2->3 = 321 // l1 : 1->2->3->4 = 4321 // summed value = 4642 value := 4642 // Create l1 : 1->2->3->4 = 4321 for i := 1; i < 5; i++ { n := datastructures.Node{} n.SetValue(i) if i == 4 { l1.Add(&n) continue } l1.Add(&n) } // Create l2 : 1->2->3 = 321 for i := 1; i < 4; i++ { n := datastructures.Node{} n.SetValue(i) l2.Add(&n) } // we can expand test cases var reverseTests = []sumLinkedListReverseTestPair{ {l1, l2, value}, } return reverseTests }
func initializeLoopedList() []findCorruptNodeTestPair { l1 := ds.LinkedList{} l2 := ds.LinkedList{} node := &ds.Node{} // Create a looped linked list // 0->1->2->3->4->5->6->3->4->5->6->3->4->5->6... for i := 0; i < 6; i++ { n := ds.Node{} n.SetValue(i) l1.Add(&n) if i == 5 { temp := l1.Head().Next().Next().Next() node = temp l1.Add(temp) } } // Create a non-looped linked list for i := 0; i < 10; i++ { n := ds.Node{} n.SetValue(i) l2.Add(&n) } // we can expand test cases var loopTests = []findCorruptNodeTestPair{ {l1, node}, {l2, nil}, // no loop } return loopTests }
func initializePartitionList() []partitionListTestPair { orig := datastructures.LinkedList{} result := datastructures.LinkedList{} value := 4 for i := 0; i < 10; i++ { n := datastructures.Node{} n.SetValue(i) // Set before values if i < value { orig.Add(&n) // Set after values } else { result.Add(&n) } } // we can expand test cases var partitionListTests = []partitionListTestPair{ {orig, result, value}, } return partitionListTests }
func TestDeleteNode(t *testing.T) { deleteNodeTests := initializeDeleteNode() for _, test := range deleteNodeTests { // save original list in a temporary list temp := datastructures.LinkedList{} for elem := range test.orig.GetElements() { n := datastructures.Node{} n.SetValue(elem) temp.Add(&n) } DeleteNode(&test.node) // item by item check through two lists for i := 0; i < test.orig.Len(); i++ { if test.orig.Find(i) != test.result.Find(i) { t.Error("For ", temp.GetElements(), "Expected", test.result.GetElements(), "got", test.orig.GetElements()) } } } }
func initializeDeleteNode() []deleteNodeTestPair { node := datastructures.Node{} orig := datastructures.LinkedList{} target := datastructures.LinkedList{} for i := 0; i < 10; i++ { n := datastructures.Node{} n.SetValue(i) orig.Add(&n) // will delete this node // so, don't add this node to result list if i == 4 { node = n } else { target.Add(&n) } } // we can expand test cases var deleteNodeTests = []deleteNodeTestPair{ {orig, target, node}, } return deleteNodeTests }
// deletes a node inside the linked list. // we assume that, we are not given access to the head of the linked list. // You only have access to that node. func DeleteNode(n *datastructures.Node) { if n.Next() != nil { n.SetValue(n.Next().GetValue()) n.SetNext(n.Next().Next()) } }