2015-03-05 05:55:53 +00:00
|
|
|
/*
|
2015-03-06 16:10:34 +00:00
|
|
|
Copyright (c) 2015, Emir Pasic
|
|
|
|
All rights reserved.
|
2015-03-05 05:55:53 +00:00
|
|
|
|
2015-03-06 16:10:34 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
2015-03-05 05:55:53 +00:00
|
|
|
|
2015-03-06 16:10:34 +00:00
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
2015-03-05 05:55:53 +00:00
|
|
|
|
2015-03-06 16:10:34 +00:00
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2015-03-05 05:55:53 +00:00
|
|
|
*/
|
|
|
|
|
2016-06-25 15:02:21 +00:00
|
|
|
// Package linkedliststack implements a stack backed by a singly-linked list.
|
|
|
|
//
|
2015-03-05 05:55:53 +00:00
|
|
|
// Structure is not thread safe.
|
2016-06-25 15:02:21 +00:00
|
|
|
//
|
|
|
|
// Reference:https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29#Linked_list
|
2015-03-05 05:55:53 +00:00
|
|
|
package linkedliststack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-22 04:04:14 +00:00
|
|
|
"github.com/emirpasic/gods/containers"
|
2015-03-10 03:31:01 +00:00
|
|
|
"github.com/emirpasic/gods/lists/singlylinkedlist"
|
2015-03-05 16:19:20 +00:00
|
|
|
"github.com/emirpasic/gods/stacks"
|
2015-03-05 06:01:59 +00:00
|
|
|
"strings"
|
2015-03-05 05:55:53 +00:00
|
|
|
)
|
|
|
|
|
2015-03-05 16:19:20 +00:00
|
|
|
func assertInterfaceImplementation() {
|
2016-06-22 01:09:48 +00:00
|
|
|
var _ stacks.Stack = (*Stack)(nil)
|
2016-06-22 17:59:08 +00:00
|
|
|
var _ containers.IteratorWithIndex = (*Iterator)(nil)
|
2015-03-05 16:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Stack holds elements in a singly-linked-list
|
2015-03-05 05:55:53 +00:00
|
|
|
type Stack struct {
|
2015-03-10 03:31:01 +00:00
|
|
|
list *singlylinkedlist.List
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// New nnstantiates a new empty stack
|
2015-03-05 05:55:53 +00:00
|
|
|
func New() *Stack {
|
2015-03-10 03:31:01 +00:00
|
|
|
return &Stack{list: &singlylinkedlist.List{}}
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Push adds a value onto the top of the stack
|
2015-03-05 05:55:53 +00:00
|
|
|
func (stack *Stack) Push(value interface{}) {
|
2015-03-10 03:31:01 +00:00
|
|
|
stack.list.Prepend(value)
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Pop removes top element on stack and returns it, or nil if stack is empty.
|
2015-03-05 05:55:53 +00:00
|
|
|
// Second return parameter is true, unless the stack was empty and there was nothing to pop.
|
|
|
|
func (stack *Stack) Pop() (value interface{}, ok bool) {
|
2015-03-10 03:31:01 +00:00
|
|
|
value, ok = stack.list.Get(0)
|
|
|
|
stack.list.Remove(0)
|
|
|
|
return
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Peek returns top element on the stack without removing it, or nil if stack is empty.
|
2015-03-05 05:55:53 +00:00
|
|
|
// Second return parameter is true, unless the stack was empty and there was nothing to peek.
|
|
|
|
func (stack *Stack) Peek() (value interface{}, ok bool) {
|
2015-03-10 03:31:01 +00:00
|
|
|
return stack.list.Get(0)
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Empty returns true if stack does not contain any elements.
|
2015-03-05 05:55:53 +00:00
|
|
|
func (stack *Stack) Empty() bool {
|
2015-03-10 03:31:01 +00:00
|
|
|
return stack.list.Empty()
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Size returns number of elements within the stack.
|
2015-03-05 05:55:53 +00:00
|
|
|
func (stack *Stack) Size() int {
|
2015-03-10 03:31:01 +00:00
|
|
|
return stack.list.Size()
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Clear removes all elements from the stack.
|
2015-03-05 21:37:25 +00:00
|
|
|
func (stack *Stack) Clear() {
|
2015-03-10 03:31:01 +00:00
|
|
|
stack.list.Clear()
|
2015-03-05 21:37:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Values returns all elements in the stack (LIFO order).
|
2015-03-07 17:05:34 +00:00
|
|
|
func (stack *Stack) Values() []interface{} {
|
2015-03-10 03:31:01 +00:00
|
|
|
return stack.list.Values()
|
2015-03-07 17:05:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Iterator returns a stateful iterator whose values can be fetched by an index.
|
2016-06-22 04:04:14 +00:00
|
|
|
type Iterator struct {
|
|
|
|
stack *Stack
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Iterator returns a stateful iterator whose values can be fetched by an index.
|
2016-06-22 04:04:14 +00:00
|
|
|
func (stack *Stack) Iterator() Iterator {
|
|
|
|
return Iterator{stack: stack, index: -1}
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Next moves the iterator to the next element and returns true if there was a next element in the container.
|
2016-06-23 22:08:04 +00:00
|
|
|
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
|
2016-06-26 18:50:49 +00:00
|
|
|
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
|
2016-06-23 22:08:04 +00:00
|
|
|
// Modifies the state of the iterator.
|
2016-06-22 04:04:14 +00:00
|
|
|
func (iterator *Iterator) Next() bool {
|
2016-06-25 17:26:08 +00:00
|
|
|
if iterator.index < iterator.stack.Size() {
|
|
|
|
iterator.index++
|
|
|
|
}
|
2016-06-22 04:04:14 +00:00
|
|
|
return iterator.stack.withinRange(iterator.index)
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Value returns the current element's value.
|
2016-06-23 22:08:04 +00:00
|
|
|
// Does not modify the state of the iterator.
|
2016-06-22 04:04:14 +00:00
|
|
|
func (iterator *Iterator) Value() interface{} {
|
|
|
|
value, _ := iterator.stack.list.Get(iterator.index) // in reverse (LIFO)
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Index returns the current element's index.
|
2016-06-23 22:08:04 +00:00
|
|
|
// Does not modify the state of the iterator.
|
2016-06-22 17:59:08 +00:00
|
|
|
func (iterator *Iterator) Index() int {
|
2016-06-22 04:04:14 +00:00
|
|
|
return iterator.index
|
|
|
|
}
|
|
|
|
|
2016-06-26 18:50:49 +00:00
|
|
|
// Reset sets the iterator to the initial state.
|
|
|
|
// Call Next() to fetch the first element if any.
|
|
|
|
func (iterator *Iterator) Reset() {
|
|
|
|
iterator.index = -1
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// String returns a string representation of container
|
2015-03-05 05:55:53 +00:00
|
|
|
func (stack *Stack) String() string {
|
2015-03-05 15:29:33 +00:00
|
|
|
str := "LinkedListStack\n"
|
2015-03-07 17:29:48 +00:00
|
|
|
values := []string{}
|
2015-03-10 03:31:01 +00:00
|
|
|
for _, value := range stack.list.Values() {
|
2015-03-07 17:29:48 +00:00
|
|
|
values = append(values, fmt.Sprintf("%v", value))
|
2015-03-05 05:55:53 +00:00
|
|
|
}
|
2015-03-07 17:29:48 +00:00
|
|
|
str += strings.Join(values, ", ")
|
2015-03-05 05:55:53 +00:00
|
|
|
return str
|
|
|
|
}
|
2016-06-22 04:04:14 +00:00
|
|
|
|
2016-06-24 18:27:34 +00:00
|
|
|
// Check that the index is within bounds of the list
|
2016-06-22 04:04:14 +00:00
|
|
|
func (stack *Stack) withinRange(index int) bool {
|
|
|
|
return index >= 0 && index < stack.list.Size()
|
|
|
|
}
|