File: stack.go

package info (click to toggle)
golang-github-shenwei356-util 0.0~git20201231.861956c-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 192 kB
  • sloc: makefile: 2
file content (44 lines) | stat: -rw-r--r-- 697 bytes parent folder | download | duplicates (3)
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
package stack

// Stack struct
type Stack []interface{}

// Empty tell if it is empty
func (s Stack) Empty() bool {
	return len(s) == 0
}

// Peek return the last element
func (s Stack) Peek() interface{} {
	return s[len(s)-1]
}

// Put puts element to stack
func (s *Stack) Put(i interface{}) {
	(*s) = append((*s), i)
}

// Pop pops element from the stack
func (s *Stack) Pop() interface{} {
	d := (*s)[len(*s)-1]
	(*s) = (*s)[:len(*s)-1]
	return d
}

/*
func main() {
	var s stack

	for i := 0; i < 3; i++ {
		s.Put(i)
		fmt.Printf("len=%d\n", len(s))
		fmt.Printf("peek=%d\n", s.Peek())
	}

	for !s.Empty() {
		i := s.Pop()
		fmt.Printf("len=%d\n", len(s))
		fmt.Printf("pop=%d\n", i)
	}
}
*/