forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
58 lines (48 loc) · 1.08 KB
/
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
//Stack in Go
package main
import "fmt"
type stack []int
var limit = 5
func (stk *stack) pushStack(ele int) {
if !((*stk).isFull()) {
*stk = append(*stk, ele)
fmt.Printf("Added element: %v\n", ele)
} else {
fmt.Println("Stack is full!")
}
}
func (stk *stack) popStack() {
if !((*stk).isEmpty()) {
top_ele, top := (*stk).peekStack()
fmt.Printf("Popped Element: %v\n", top_ele)
(*stk) = (*stk)[:top]
} else {
fmt.Println("Stack is empty!")
}
}
func (stk *stack) isEmpty() bool {
return len(*stk) == 0
}
func (stk *stack) isFull() bool {
return len(*stk) == limit
}
func (stk *stack) peekStack() (int, int) {
top := len(*stk) - 1
top_ele := (*stk)[top]
return top_ele, top
}
func main() {
fmt.Println("Welcome to Stack program in Go!")
var stack1 stack
stack1.pushStack(4)
stack1.pushStack(5)
stack1.pushStack(1)
stack1.pushStack(3)
stack1.pushStack(7)
stack1.pushStack(9)
stack1.popStack()
stack1.popStack()
stack1.popStack()
topOfStack, topIndex := stack1.peekStack()
fmt.Printf("Top element of stack: %v\tIndex of top element: %v\n", topOfStack, topIndex)
}