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
|
|
|
*/
|
|
|
|
|
|
|
|
// Implementation of stack using linked list.
|
|
|
|
// Used by red-black tree during in-order traversal.
|
|
|
|
// Structure is not thread safe.
|
|
|
|
// References: http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29
|
|
|
|
|
|
|
|
package linkedliststack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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() {
|
|
|
|
var _ stacks.Interface = (*Stack)(nil)
|
|
|
|
}
|
|
|
|
|
2015-03-05 05:55:53 +00:00
|
|
|
type Stack struct {
|
|
|
|
top *element
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
type element struct {
|
|
|
|
value interface{}
|
|
|
|
next *element
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiates a new empty stack
|
|
|
|
func New() *Stack {
|
|
|
|
return &Stack{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pushes a value onto the top of the stack
|
|
|
|
func (stack *Stack) Push(value interface{}) {
|
|
|
|
stack.top = &element{value, stack.top}
|
|
|
|
stack.size += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pops (removes) top element on stack and returns it, or nil if stack is empty.
|
|
|
|
// Second return parameter is true, unless the stack was empty and there was nothing to pop.
|
|
|
|
func (stack *Stack) Pop() (value interface{}, ok bool) {
|
|
|
|
if stack.size > 0 {
|
|
|
|
value, stack.top = stack.top.value, stack.top.next
|
|
|
|
stack.size -= 1
|
|
|
|
return value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns top element on the stack without removing it, or nil if stack is empty.
|
|
|
|
// Second return parameter is true, unless the stack was empty and there was nothing to peek.
|
|
|
|
func (stack *Stack) Peek() (value interface{}, ok bool) {
|
|
|
|
if stack.size > 0 {
|
|
|
|
return stack.top.value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2015-03-05 06:01:59 +00:00
|
|
|
// Returns true if stack does not contain any elements.
|
2015-03-05 05:55:53 +00:00
|
|
|
func (stack *Stack) Empty() bool {
|
|
|
|
return stack.size == 0
|
|
|
|
}
|
|
|
|
|
2015-03-05 06:01:59 +00:00
|
|
|
// Returns number of elements within the stack.
|
2015-03-05 05:55:53 +00:00
|
|
|
func (stack *Stack) Size() int {
|
|
|
|
return stack.size
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:37:25 +00:00
|
|
|
// Removes all elements from the stack.
|
|
|
|
func (stack *Stack) Clear() {
|
|
|
|
stack.top = nil
|
|
|
|
stack.size = 0
|
|
|
|
}
|
|
|
|
|
2015-03-07 17:05:34 +00:00
|
|
|
// Returns all elements in the stack (LIFO order).
|
|
|
|
func (stack *Stack) Values() []interface{} {
|
2015-03-07 17:19:58 +00:00
|
|
|
elements := make([]interface{}, stack.size, stack.size)
|
2015-03-07 17:05:34 +00:00
|
|
|
element := stack.top
|
|
|
|
for index := 0; element != nil; index++ {
|
2015-03-07 17:19:58 +00:00
|
|
|
elements[index] = element.value
|
2015-03-07 17:05:34 +00:00
|
|
|
element = element.next
|
|
|
|
}
|
|
|
|
|
|
|
|
return elements
|
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
for _, value := range stack.Values() {
|
|
|
|
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
|
|
|
|
}
|