Skip to content

Commit f3423ca

Browse files
committed
first commit
0 parents  commit f3423ca

File tree

3 files changed

+273
-0
lines changed

3 files changed

+273
-0
lines changed

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# prerequisite: GOROOT and GOARCH must be defined
2+
3+
# defines $(GC) (compiler), $(LD) (linker) and $(O) (architecture)
4+
include $(GOROOT)/src/Make.$(GOARCH)
5+
6+
# name of the package (library) being built
7+
TARG=vtgostack
8+
9+
GOFILES=\
10+
stack.go\
11+
12+
include $(GOROOT)/src/Make.pkg

stack.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
2+
package vtgostack
3+
4+
5+
type Entry interface{}
6+
7+
8+
9+
type Stack interface {
10+
Push(e Entry)
11+
Pop() Entry
12+
Top() Entry
13+
Depth() int
14+
RemCapacity() int
15+
Clear()
16+
}
17+
18+
/* Adding flipping capability using interface embedding */
19+
type FlippingStack interface {
20+
Stack
21+
Flip() FlippingStack
22+
}
23+
24+
25+
26+
/* Implementation of stack using array */
27+
type ArrayStack struct {
28+
data []Entry
29+
top int
30+
}
31+
32+
// Creates new ArrayStack with specified maximum depth (capacity)
33+
func NewArrayStack(maxDepth int) *ArrayStack {
34+
return &ArrayStack{ make([]Entry, maxDepth + 1), 0}
35+
}
36+
37+
38+
// Methods
39+
40+
func (s *ArrayStack) Push(e Entry) {
41+
if s.top < len(s.data) - 1 {
42+
s.top++
43+
s.data[s.top] = e
44+
}
45+
}
46+
47+
func (s *ArrayStack) Top() Entry {
48+
if s.top > 0 {
49+
return s.data[s.top]
50+
}
51+
return nil
52+
}
53+
54+
55+
func (s *ArrayStack) Pop() Entry {
56+
var retVal Entry = nil
57+
if s.top > 0 {
58+
retVal = s.data[s.top]
59+
s.top--
60+
}
61+
return retVal
62+
}
63+
64+
func (s *ArrayStack) Depth() int {
65+
return s.top
66+
}
67+
68+
func (s *ArrayStack) RemCapacity() int {
69+
return len(s.data) - s.top - 1
70+
}
71+
72+
func (s *ArrayStack) Clear() {
73+
s.top = 0
74+
}
75+
76+
func (s *ArrayStack) MaxDepth() int {
77+
return len(s.data) - 1
78+
}
79+
80+
81+
82+
/* Implementation of FlippingStack */
83+
type FlippingArrayStack struct {
84+
*ArrayStack // Embedded
85+
}
86+
87+
func (s *FlippingArrayStack) Flip() FlippingStack {
88+
flipped := NewArrayStack(s.MaxDepth())
89+
for s.Depth() > 0 {
90+
flipped.Push(s.Pop())
91+
}
92+
return &FlippingArrayStack{ flipped }
93+
}
94+
95+
96+
97+
/* Stack implementation using linked list */
98+
99+
type element struct {
100+
next *element
101+
value Entry
102+
}
103+
104+
type LinkedListStack struct {
105+
top *element
106+
maxDepth int
107+
depth int
108+
}
109+
110+
// Create new LinkedListStack with specified maximum depth
111+
func NewLinkedListStack(maxDepth int) *LinkedListStack {
112+
return &LinkedListStack{ nil, maxDepth, 0 }
113+
}
114+
115+
func (s *LinkedListStack) Push(e Entry) {
116+
if s.top == nil {
117+
s.top = &element{ nil, e}
118+
s.depth++
119+
} else if (s.depth < s.maxDepth) {
120+
newTop := &element{ s.top, e }
121+
s.top = newTop
122+
s.depth++
123+
}
124+
}
125+
126+
127+
func (s *LinkedListStack) Top() Entry {
128+
if s.top == nil {
129+
return nil
130+
}
131+
return s.top.value
132+
}
133+
134+
func (s *LinkedListStack) Pop() Entry {
135+
if s.top == nil {
136+
return nil
137+
}
138+
retVal := s.top.value
139+
s.top = s.top.next
140+
s.depth--
141+
return retVal
142+
}
143+
144+
func (s *LinkedListStack) Depth() int {
145+
return s.depth
146+
}
147+
148+
func (s *LinkedListStack) RemCapacity() int {
149+
return s.maxDepth - s.depth
150+
}
151+
152+
func (s *LinkedListStack) Clear() {
153+
s.top = nil
154+
s.depth = 0
155+
}
156+
157+
func (s *LinkedListStack) MaxDepth() int {
158+
return s.maxDepth
159+
}
160+
161+
162+
163+
164+
165+
166+
type FlippingLinkedListStack struct {
167+
*LinkedListStack // Embedded
168+
}
169+
170+
171+
func (s *FlippingLinkedListStack) Flip() FlippingStack {
172+
flipped := NewLinkedListStack(s.MaxDepth())
173+
for s.Depth() > 0 {
174+
flipped.Push(s.Pop())
175+
}
176+
return &FlippingLinkedListStack{ flipped }
177+
}
178+
179+

stack_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
package vtgostack
3+
4+
import "testing"
5+
6+
7+
func TestArrayStack(t *testing.T) {
8+
var arrayStack Stack = NewArrayStack(3)
9+
testImpl(t, arrayStack, 3)
10+
}
11+
12+
func TestLinkedListStack(t *testing.T) {
13+
linkedListStack := NewLinkedListStack(3)
14+
testImpl(t, linkedListStack, 3)
15+
}
16+
17+
func assertValue(t *testing.T, expected int, actual int, message string) {
18+
if expected != actual {
19+
t.Errorf("expected %s, actual %s %s", expected, actual, message)
20+
}
21+
}
22+
23+
func testImpl(t *testing.T, s Stack, maxDepth int) {
24+
if s.Depth() != 0 {
25+
t.Errorf("Initial stack has depth %d", s.Depth())
26+
return
27+
}
28+
assertValue(t, maxDepth, s.RemCapacity(), "RemCapacity()")
29+
s.Push(1)
30+
assertValue(t, 1, s.Top().(int), " Top()")
31+
assertValue(t, 1, s.Depth(), "Depth()")
32+
assertValue(t, 2, s.RemCapacity(), "RemCapacity()")
33+
34+
s.Push(3)
35+
assertValue(t, 3, s.Top().(int), " Top()")
36+
assertValue(t, 2, s.Depth(), "Depth()")
37+
assertValue(t, 1, s.RemCapacity(), "RemCapacity()")
38+
39+
s.Push(5)
40+
assertValue(t, 3, s.Depth(), "Depth()")
41+
assertValue(t, 0, s.RemCapacity(), "RemCapacity()")
42+
43+
s.Push(6) // does nothing
44+
assertValue(t, 5, s.Top().(int), " Top()")
45+
assertValue(t, 5, s.Pop().(int), " Pop()")
46+
assertValue(t, 2, s.Depth(), "Depth()")
47+
assertValue(t, 1, s.RemCapacity(), "RemCapacity()")
48+
49+
50+
assertValue(t, 3, s.Pop().(int), " Pop()")
51+
assertValue(t, 1, s.Depth(), "Depth()")
52+
assertValue(t, 2, s.RemCapacity(), "RemCapacity()")
53+
54+
assertValue(t, 1, s.Pop().(int), " Pop()")
55+
assertValue(t, 0, s.Depth(), "Depth()")
56+
assertValue(t, 3, s.RemCapacity(), "RemCapacity()")
57+
}
58+
59+
60+
func TestFlip(t *testing.T) {
61+
var flippingStack FlippingStack = &FlippingArrayStack{ NewArrayStack(3) }
62+
testFlip(t, flippingStack)
63+
64+
flippingStack = &FlippingLinkedListStack{ NewLinkedListStack(3) }
65+
testFlip(t, flippingStack)
66+
}
67+
68+
69+
func testFlip(t *testing.T, flipping FlippingStack) {
70+
flipping.Push(1)
71+
flipping.Push(2)
72+
flipping.Push(3)
73+
74+
flipped := flipping.Flip()
75+
assertValue(t, 1, flipped.Pop().(int), "Pop()")
76+
assertValue(t, 2, flipped.Pop().(int), "Pop()")
77+
assertValue(t, 3, flipped.Pop().(int), "Pop()")
78+
assertValue(t, 0, flipped.Depth(), "Depth()")
79+
}
80+
81+
82+

0 commit comments

Comments
 (0)