-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package day53 | ||
|
||
import "errors" | ||
|
||
var errEmpty = errors.New("empty") | ||
|
||
// ErrEmpty is the error returned for an empty data structure. | ||
func ErrEmpty() error { | ||
return errEmpty | ||
} | ||
|
||
// Stack represents a LIFO data structure. | ||
type Stack interface { | ||
Push(val interface{}) | ||
Pop() (interface{}, error) | ||
} | ||
|
||
// Queue represents a FIFO data structure. | ||
type Queue interface { | ||
Enqueue(item interface{}) | ||
Dequeue() (interface{}, error) | ||
} | ||
|
||
type stack struct { | ||
values []interface{} | ||
} | ||
|
||
func (s *stack) Push(val interface{}) { | ||
s.values = append(s.values, val) | ||
} | ||
|
||
func (s *stack) Pop() (interface{}, error) { | ||
if len(s.values) == 0 { | ||
return nil, ErrEmpty() | ||
} | ||
var x interface{} | ||
x, s.values = s.values[len(s.values)-1], s.values[:len(s.values)-1] | ||
return x, nil | ||
} | ||
|
||
type twoStacksQueue struct { | ||
enqueue, dequeue *stack | ||
} | ||
|
||
func (tsq twoStacksQueue) Enqueue(item interface{}) { | ||
tsq.enqueue.Push(item) | ||
} | ||
|
||
func (tsq twoStacksQueue) Dequeue() (interface{}, error) { | ||
if val, err := tsq.dequeue.Pop(); err == nil { | ||
return val, nil | ||
} | ||
for val, err := tsq.enqueue.Pop(); err == nil; val, err = tsq.enqueue.Pop() { | ||
tsq.dequeue.Push(val) | ||
} | ||
if val, err := tsq.dequeue.Pop(); err == nil { | ||
return val, nil | ||
} | ||
return nil, ErrEmpty() | ||
} | ||
|
||
// NewQueue returns a queue. FIFO. | ||
func NewQueue() Queue { | ||
return &twoStacksQueue{&stack{}, &stack{}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package day53 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
inserts []interface{} | ||
}{ | ||
{[]interface{}{"foo", "bar", 1, 3, 5, 6}}, | ||
} | ||
|
||
func TestQueue(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
q := NewQueue() | ||
for _, v := range tc.inserts { | ||
q.Enqueue(v) | ||
} | ||
for _, v := range tc.inserts { | ||
if res, err := q.Dequeue(); err != nil { | ||
t.Errorf("Expected no errors while dequeuing") | ||
} else if res != v { | ||
t.Errorf("Expected results in FIFO order. Expected %v, got %v", v, res) | ||
} | ||
} | ||
if _, err := q.Dequeue(); err == nil { | ||
t.Errorf("Dequeue on empty queue should return an error") | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkQueue(b *testing.B) { | ||
} |