forked from gcsrilanka/Algorithm-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.go
44 lines (34 loc) · 842 Bytes
/
Stack.go
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 main
import "fmt"
type Stack []string
// isEmpty: check if stack is empty
func (s *Stack) isEmpty() bool {
return len(*s) == 0
}
// push a new value onto the stack
func (s *Stack) push(str string) {
*s = append(*s, str)
}
// Remove and return top element of stack. Return false if stack is empty.
func (s *Stack) pop() (string, bool) {
if s.isEmpty() {
return "", false
} else {
index := len(*s) - 1
element := (*s)[index]
*s = (*s)[:index]
return element, true
}
}
func main() {
var stack Stack // create a stack variable of type Stack
stack.push("Apple")
stack.push("Orange")
stack.push("Grapes")
stack.push("Watermelons")
stack.push("Papaya")
fmt.Println(stack) // Output :- [Apple Orange Grapes Watermelons Papaya]
stack.pop()
stack.pop()
fmt.Println(stack) // Output :- [Apple Orange Grapes]
}