-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
179 lines (130 loc) · 2.92 KB
/
Copy pathstack.go
File metadata and controls
179 lines (130 loc) · 2.92 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
package vtgostack
type Entry interface{}
type Stack interface {
Push(e Entry)
Pop() Entry
Top() Entry
Depth() int
RemCapacity() int
Clear()
}
/* Adding flipping capability using interface embedding */
type FlippingStack interface {
Stack
Flip() FlippingStack
}
/* Implementation of stack using array */
type ArrayStack struct {
data []Entry
top int
}
// Creates new ArrayStack with specified maximum depth (capacity)
func NewArrayStack(maxDepth int) *ArrayStack {
return &ArrayStack{ make([]Entry, maxDepth + 1), 0}
}
// Methods
func (s *ArrayStack) Push(e Entry) {
if s.top < len(s.data) - 1 {
s.top++
s.data[s.top] = e
}
}
func (s *ArrayStack) Top() Entry {
if s.top > 0 {
return s.data[s.top]
}
return nil
}
func (s *ArrayStack) Pop() Entry {
var retVal Entry = nil
if s.top > 0 {
retVal = s.data[s.top]
s.top--
}
return retVal
}
func (s *ArrayStack) Depth() int {
return s.top
}
func (s *ArrayStack) RemCapacity() int {
return len(s.data) - s.top - 1
}
func (s *ArrayStack) Clear() {
s.top = 0
}
func (s *ArrayStack) MaxDepth() int {
return len(s.data) - 1
}
/* Implementation of FlippingStack */
type FlippingArrayStack struct {
*ArrayStack // Embedded
}
func (s *FlippingArrayStack) Flip() FlippingStack {
flipped := NewArrayStack(s.MaxDepth())
for s.Depth() > 0 {
flipped.Push(s.Pop())
}
return &FlippingArrayStack{ flipped }
}
/* Stack implementation using linked list */
type element struct {
next *element
value Entry
}
type LinkedListStack struct {
top *element
maxDepth int
depth int
}
// Create new LinkedListStack with specified maximum depth
func NewLinkedListStack(maxDepth int) *LinkedListStack {
return &LinkedListStack{ nil, maxDepth, 0 }
}
func (s *LinkedListStack) Push(e Entry) {
if s.top == nil {
s.top = &element{ nil, e}
s.depth++
} else if (s.depth < s.maxDepth) {
newTop := &element{ s.top, e }
s.top = newTop
s.depth++
}
}
func (s *LinkedListStack) Top() Entry {
if s.top == nil {
return nil
}
return s.top.value
}
func (s *LinkedListStack) Pop() Entry {
if s.top == nil {
return nil
}
retVal := s.top.value
s.top = s.top.next
s.depth--
return retVal
}
func (s *LinkedListStack) Depth() int {
return s.depth
}
func (s *LinkedListStack) RemCapacity() int {
return s.maxDepth - s.depth
}
func (s *LinkedListStack) Clear() {
s.top = nil
s.depth = 0
}
func (s *LinkedListStack) MaxDepth() int {
return s.maxDepth
}
type FlippingLinkedListStack struct {
*LinkedListStack // Embedded
}
func (s *FlippingLinkedListStack) Flip() FlippingStack {
flipped := NewLinkedListStack(s.MaxDepth())
for s.Depth() > 0 {
flipped.Push(s.Pop())
}
return &FlippingLinkedListStack{ flipped }
}