forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.go
95 lines (80 loc) · 1.83 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"errors"
"fmt"
"math/rand"
)
// A linked list node
type node struct {
data interface{}
next *node
}
type stack struct {
top *node
size int
}
func (obj *stack) isEmpty() bool {
return obj.size == 0
}
// Adds the given element to the beginning of the stack
func (obj *stack) Push(value interface{}) {
obj.top = &node{
data: value,
next: obj.top,
}
obj.size++
}
// Removes the element at the beginning of the stack
// and returns the element along with an error message
// if the stack is empty else 'nil'
func (obj *stack) Pop() (interface{}, error) {
if obj.top == nil {
return nil, errors.New("Empty stack")
}
var data interface{}
data, obj.top = obj.top.data, obj.top.next
obj.size--
return data, nil
}
// Returns a random element to insert in the Stack
func getElement() interface{} {
var temp interface{} = rand.Intn(100)
fmt.Println("Element: ", temp)
return temp
}
// Returns the value at the beginning of the stack
// along with the error message
func (obj *stack) Peek() (interface{}, error) {
if obj.top == nil {
return nil, errors.New("Empty stack")
}
return obj.top.data, nil
}
func main() {
var object stack
var flag bool = true
var choice int
for flag {
fmt.Print("Menu\n1. Push element\n2. Pop element\n3. Peek element\n4. Count of elements\n5. Exit\nEnter your choice: ")
fmt.Scan(&choice)
switch choice {
case 1:
element := getElement()
object.Push(element)
case 2:
element, err := object.Pop()
fmt.Println("Element: ", element)
fmt.Println("Error: ", err)
case 3:
element, err := object.Peek()
fmt.Println("Element: ", element)
fmt.Println("Error: ", err)
case 4:
fmt.Println("The total number of elements present in the Stack are: ", object.size)
case 5:
flag = false
default:
fmt.Println("Please enter valid choice.")
}
}
}