本文整理汇总了Golang中ctci/datastructures.Node类的典型用法代码示例。如果您正苦于以下问题:Golang Node类的具体用法?Golang Node怎么用?Golang Node使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Node类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: initializePalindromeTests
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
}
开发者ID:muraty,项目名称:ctci,代码行数:31,代码来源:2.7_test.go
示例2: PartitionList
// 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
}
开发者ID:muraty,项目名称:ctci,代码行数:20,代码来源:2.4.go
示例3: initializeForwardList
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
}
开发者ID:muraty,项目名称:ctci,代码行数:28,代码来源:2.5_test.go
示例4: initializeReverseList
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
}
开发者ID:muraty,项目名称:ctci,代码行数:30,代码来源:2.5_test.go
示例5: initializeLoopedList
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
}
开发者ID:muraty,项目名称:ctci,代码行数:31,代码来源:2.6_test.go
示例6: initializePartitionList
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
}
开发者ID:muraty,项目名称:ctci,代码行数:22,代码来源:2.4_test.go
示例7: TestDeleteNode
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())
}
}
}
}
开发者ID:muraty,项目名称:ctci,代码行数:22,代码来源:2.3_test.go
示例8: initializeDeleteNode
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
}
开发者ID:muraty,项目名称:ctci,代码行数:23,代码来源:2.3_test.go
示例9: DeleteNode
// 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())
}
}
开发者ID:muraty,项目名称:ctci,代码行数:9,代码来源:2.3.go
注:本文中的ctci/datastructures.Node类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论