-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.go
58 lines (49 loc) · 843 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package data_structure_go
import "fmt"
type node_stack struct {
Data string
Next *node_stack
}
type Stack struct {
Top *node_stack
}
func (s *Stack) IsEmpty() bool {
return s.Top == nil
}
func (s *Stack) CountElement() int {
temp := s.Top
jumlah := 0
if temp == nil {
return jumlah
}
for temp != nil {
jumlah++
temp = temp.Next
}
return jumlah
}
func (s *Stack) CreateEmpty() {
s.Top = nil
}
func (s *Stack) Push(input string) {
node_stackIn := &node_stack{Data: input}
if s.Top == nil {
s.Top = node_stackIn
return
}
node_stackIn.Next = s.Top
s.Top = node_stackIn
}
func (s *Stack) Pop() {
temp := s.Top
temp2 := s.Top.Data
s.Top = temp.Next
fmt.Println("Data dibuang ->", temp2)
}
func (s *Stack) Print() {
temp := s.Top
for temp != nil {
fmt.Println("Data : ", temp.Data)
temp = temp.Next
}
}