-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
190 lines (149 loc) · 3.03 KB
/
stack.go
File metadata and controls
190 lines (149 loc) · 3.03 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"errors"
"fmt"
)
type Stack struct{
items[] interface{}
}
//initialize from array
func (s *Stack) fromArray(array []interface{}) error{
if len(array) == 0 {
return errors.New("Cannot initialize a stack from an empty array")
}
s.items = array
return nil
}
func (s *Stack) clone() *Stack{
newItems := make([]interface{}, len(s.items))
copy(newItems, s.items)
return &Stack{
items: newItems,
}
}
func (s *Stack) Push(item interface{}){
s.items = append(s.items, item)
}
func (s *Stack) Pop() (interface{}, error){
if s.isEmpty(){
return nil, errors.New("Stack is empty")
}else{
poppedItem := s.items[len(s.items)-1]
s.items = s.items[:len(s.items)-1]
return poppedItem, nil
}
}
func (s *Stack) DeleteItem(item interface{}) (error){
if s.isEmpty(){
return errors.New("Stack is empty")
}
index, err := s.IndexOf(item)
if index == -1 || err != nil{
return err
}else{
s.items = append(s.items[:index], s.items[index+1:]...)
return nil
}
}
func (s *Stack) IndexOf(item interface{}) (int, error){
if s.isEmpty(){
return -1, errors.New("Stack is empty")
}else{
for i, v := range s.items{
if(v == item){
return i, nil
}
}
return -1, nil
}
}
func (s *Stack) Peek() (interface{}, error){
if s.isEmpty(){
return nil, errors.New("Stack is empty")
}else{
return s.items[len(s.items)-1], nil
}
}
func (s *Stack) Bottom() (interface{}, error){
if s.isEmpty(){
return nil, errors.New("Stack is empty")
}else{
return s.items[0], nil
}
}
func (s *Stack) Clear() (error){
if s.isEmpty(){
return errors.New("Stack is empty")
}else{
s.items = []interface{}{}
return nil
}
}
func (s *Stack) Reverse() (error){
if s.isEmpty(){
return errors.New("Stack is Empty")
}
for i, j := 0, len(s.items)-1; i<j; i, j = i+1, j-1 {
s.items[i], s.items[j] = s.items[j], s.items[i]
}
return nil
}
func (s *Stack) Contains(item interface{}) bool{
if s.isEmpty(){
return false
}else{
for _, v := range s.items{
if item == v{
return true
}
}
return false
}
}
func (s *Stack) Size(item interface{}) (int){
return len(s.items)
}
func (s *Stack) isEmpty() bool{
return len(s.items) == 0
}
func (s *Stack) Print(direction string){
if(s.isEmpty()){
fmt.Println("Stack is empty")
}else if direction != "v" && direction != "h"{
fmt.Println("Please input a correct direction")
return
}else{
for i := len(s.items)-1; i >= 0; i--{
if direction != "v"{
fmt.Print(s.items[i])
if i != 0{
fmt.Print("->")
}else{
fmt.Print("\n")
}
}else{
fmt.Println(s.items[i])
}
}
}
}
func main(){
stack := &Stack{}
stack.fromArray([]interface{}{1, 2, 3, 4,"5"})
// stack.Push(1)
// stack.Push(2)
// stack.Push("5")
// stack.Push(4)
// index, _ := stack.IndexOf("5")
// stack.Peek()
// stack.DeleteItem(4)
// stack.Clear()
// stack.Print("h")
// stack.Reverse()
// fmt.Println(stack.Contains("1"))
// clonedStack := stack.clone()
// clonedStack.Reverse()
// stack.Print("h")
// clonedStack.Print("h")
stack.Print("v")
}