dengxinyi 6 年之前
父節點
當前提交
6c22444305
共有 1 個文件被更改,包括 51 次插入0 次删除
  1. 51 0
      hard/stack.go

+ 51 - 0
hard/stack.go

@@ -0,0 +1,51 @@
+package main
+
+// Stack ...
+type Stack []int
+
+// Top ...
+func (s Stack) Top() int {
+	return s[len(s)-1]
+}
+
+// Empty ...
+func (s Stack) Empty() bool {
+	return len(s) == 0
+}
+
+// Push ...
+func (s *Stack) Push(val int) {
+	*s = append(*s, val)
+}
+
+// Pop ...
+func (s *Stack) Pop() int {
+	top := s.Top()
+	*s = (*s)[:len(*s)-1]
+	return top
+}
+
+// Stack64 ...
+type Stack64 []int64
+
+// Top ...
+func (s Stack64) Top() int64 {
+	return s[len(s)-1]
+}
+
+// Empty ...
+func (s Stack64) Empty() bool {
+	return len(s) == 0
+}
+
+// Push ...
+func (s *Stack64) Push(val int64) {
+	*s = append(*s, val)
+}
+
+// Pop ...
+func (s *Stack64) Pop() int64 {
+	top := s.Top()
+	*s = (*s)[:len(*s)-1]
+	return top
+}