-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
86 lines (70 loc) · 1.8 KB
/
stack.go
File metadata and controls
86 lines (70 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
)
//define stack struct
type Stack []interface{}
//get stack length
func (stack Stack) len() int {
return len(stack)
}
//get stack capacity
func (stack Stack) cap() int {
return cap(stack)
}
//push value
func (stack *Stack) push(v interface{}) {
*stack = append(*stack, v)
}
//get top value not remove
func (stack Stack) top() interface{} {
if len(stack) == 0 {
fmt.Println("This stack cap is 0!")
return nil
}
return stack[len(stack)-1]
}
//get pop value and remove
func (stack *Stack) pop() interface{} {
tempStack := *stack
if len(tempStack) == 0 {
fmt.Println("This stack cap is 0!")
return nil
}
value := tempStack[len(tempStack)-1]
*stack = tempStack[:len(tempStack)-1]
return value
}
//stack is empty
func (stack Stack) isEmpty() bool {
return len(stack) == 0
}
//print stack info
func (stack Stack) printInfo() {
if stack.isEmpty() {
fmt.Println("This Stack is empty!")
return
}
for i := 0; i < stack.len(); i++ {
fmt.Printf("Stack value is: %v\n", stack[i])
}
}
func main() {
var testStack Stack
testStack.push("a") //push data
testStack.push("b")
testStack.push("c")
testStack.printInfo()
fmt.Printf("\n-------------info-show-----------------------\n")
fmt.Printf("Stack len is: %d\n", testStack.len())
fmt.Printf("Stack cap is: %d\n", testStack.cap())
fmt.Printf("Stack is empty: %v", testStack.isEmpty())
fmt.Printf("\n\n------------------top-test-----------------\n")
fmt.Printf("Stack top is: %v\n", testStack.top())
fmt.Printf("Stack len is: %d\n", testStack.len())
fmt.Printf("\n\n-----------------pop-test-------------------\n")
fmt.Printf("Stack pop is: %v\n", testStack.pop())
fmt.Printf("Stack len is: %d\n", testStack.len())
fmt.Printf("Stack pop is: %v\n", testStack.pop())
fmt.Printf("Stack len is: %d\n", testStack.len())
}