当前位置: 首页>>代码示例>>Golang>>正文


Golang Stack.Len方法代码示例

本文整理汇总了Golang中stacker/stack.Stack.Len方法的典型用法代码示例。如果您正苦于以下问题:Golang Stack.Len方法的具体用法?Golang Stack.Len怎么用?Golang Stack.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stacker/stack.Stack的用法示例。


在下文中一共展示了Stack.Len方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestStack

func TestStack(t *testing.T) {
	count := 1
	var aStack stack.Stack
	assertTrue(t, aStack.Len() == 0, "expected empty Stack", count) // 1
	count++
	assertTrue(t, aStack.Cap() == 0, "expected empty Stack", count) // 2
	count++
	assertTrue(t, aStack.IsEmpty(), "expected empty Stack", count) // 3
	count++
	value, err := aStack.Pop()
	assertTrue(t, value == nil, "expected nil value", count) // 4
	count++
	assertTrue(t, err != nil, "expected error", count) // 5
	count++
	value1, err := aStack.Top()
	assertTrue(t, value1 == nil, "expected nil value", count) // 6
	count++
	assertTrue(t, err != nil, "expected error", count) // 7
	count++
	aStack.Push(1)
	aStack.Push(2)
	aStack.Push("three")
	assertTrue(t, aStack.Len() == 3, "expected nonempty Stack", count) // 8
	count++
	assertTrue(t, aStack.IsEmpty() == false, "expected nonempty Stack",
		count) // 9
	count++
	value2, err := aStack.Pop()
	assertEqualString(t, value2.(string), "three", "unexpected text",
		count) // 10
	count++
	assertTrue(t, err == nil, "no error expected", count) // 11
	count++
	value3, err := aStack.Top()
	assertTrue(t, value3 == 2, "unexpected number", count) // 12
	count++
	assertTrue(t, err == nil, "no error expected", count) // 13
	count++
	aStack.Pop()
	assertTrue(t, aStack.Len() == 1, "expected nonempty Stack", count) //14
	count++
	assertTrue(t, aStack.IsEmpty() == false, "expected nonempty Stack",
		count) // 15
	count++
	value4, err := aStack.Pop()
	assertTrue(t, value4 == 1, "unexpected number", count) // 16
	count++
	assertTrue(t, err == nil, "no error expected", count) // 17
	count++
	assertTrue(t, aStack.Len() == 0, "expected empty Stack", count) // 18
	count++
	assertTrue(t, aStack.IsEmpty(), "expected empty Stack", count) // 19
	count++
}
开发者ID:pawelropa,项目名称:programmingingo,代码行数:54,代码来源:stack_test.go

示例2: main

func main() {
	var haystack stack.Stack
	haystack.Push("hay")
	haystack.Push(-15)
	haystack.Push([]string{"pin", "clip", "needle"})
	haystack.Push(81.52)
	for {
		item, err := haystack.Pop()
		if err != nil {
			break
		}
		fmt.Println(item)
	}

	var aStack stack.Stack
	aStack.Push("Aarvark")
	aStack.Push(5)
	aStack.Push(19)
	x, err := aStack.Top()
	fmt.Println(x)
	aStack.Push(-6e-4)
	aStack.Push("Baker")
	aStack.Push(-3)
	aStack.Push("Cake")
	aStack.Push("Dancer")
	x, err = aStack.Top()
	fmt.Println(x)
	aStack.Push(11.7)
	fmt.Println("stack is empty", aStack.IsEmpty())
	fmt.Printf("Len() == %d  Cap == %d\n", aStack.Len(), aStack.Cap())
	difference := aStack.Cap() - aStack.Len()
	for i := 0; i < difference; i++ {
		aStack.Push(strings.Repeat("*", difference-i))
	}
	fmt.Printf("Len() == %d  Cap == %d\n", aStack.Len(), aStack.Cap())
	for aStack.Len() > 0 {
		x, _ = aStack.Pop()
		fmt.Printf("%T %v\n", x, x)
	}
	fmt.Println("stack is empty", aStack.IsEmpty())
	x, err = aStack.Pop()
	fmt.Println(x, err)
	x, err = aStack.Top()
	fmt.Println(x, err)
}
开发者ID:jusonalien,项目名称:goinaction,代码行数:45,代码来源:stacker.go

示例3: main

func main() {
	var hi stack.Stack
	hi.Push("hi")
	hi.Push("hello")
	hi.Push(22)
	hi.Push([]string{"good", "morning", "bye"})
	hi.Push(-12.3)

	for {
		item, err := hi.Pop()
		if err != nil {
			break
		}
		fmt.Println(item)
	}

	var ho stack.Stack
	ho.Push("Xiaomi")
	ho.Push("Huawei")
	ho.Push(1999)
	x, err := ho.Top()
	fmt.Println(x)
	ho.Push(-6e-4)
	ho.Push("Apple")
	ho.Push("MS")
	x, err = ho.Top()
	fmt.Println(x)
	ho.Push(11.11)
	fmt.Println("stack is empty?", ho.IsEmpty())
	fmt.Printf("Len() == %d Cap == %d\n", ho.Len(), ho.Cap())
	diff := ho.Cap() - ho.Len()
	for i := 0; i < diff; i++ {
		ho.Push(strings.Repeat("*", diff-i))
	}
	fmt.Printf("Len() == %d  Cap() == %d\n", ho.Len(), ho.Cap())
	for ho.Len() > 0 {
		x, _ := ho.Pop()
		fmt.Printf("%T %v\n", x, x)
	}
	fmt.Println("stack is empty?", ho.IsEmpty())
	x, err = ho.Pop()
	fmt.Println(x, err)
	x, err = ho.Pop()
	fmt.Println(x, err)
}
开发者ID:JingkaiTang,项目名称:go-go-go,代码行数:45,代码来源:stacker.go


注:本文中的stacker/stack.Stack.Len方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。