- implemented linke list stack - minor changes on other libs - tests included

pull/1/head
Emir Pasic 11 years ago
parent 14efefae45
commit 2ac9c4d384

@ -0,0 +1,92 @@
/*
Copyright (c) Emir Pasic, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. See the file LICENSE included
with this distribution for more information.
*/
// 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"
)
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
}
func (stack *Stack) Empty() bool {
return stack.size == 0
}
func (stack *Stack) Size() int {
return stack.size
}
func (stack *Stack) String() string {
str := "RedBlackTree\n"
element := stack.top
elementsValues := []string{}
for element != nil {
elementsValues = append(elementsValues, fmt.Sprintf("%v ", element.value))
element = element.next
}
for i, j := 0, len(elementsValues)-1; i < j; i, j = i+1, j-1 {
elementsValues[i], elementsValues[j] = elementsValues[j], elementsValues[i]
}
str += fmt.Sprintf("#v", elementsValues)
return str
}

@ -0,0 +1,72 @@
/*
Copyright (c) Emir Pasic, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. See the file LICENSE included
with this distribution for more information.
*/
package linkedliststack
import (
"testing"
)
func TestLinkedListStack(t *testing.T) {
stack := New()
if actualValue := stack.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
// insertions
stack.Push(1)
stack.Push(2)
stack.Push(3)
if actualValue := stack.Empty(); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := stack.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, ok := stack.Peek(); actualValue != 3 || !ok {
t.Errorf("Got %v expected %v", actualValue, 3)
}
stack.Pop()
if actualValue, ok := stack.Peek(); actualValue != 2 || !ok {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := stack.Pop(); actualValue != 2 || !ok {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := stack.Pop(); actualValue != 1 || !ok {
t.Errorf("Got %v expected %v", actualValue, 1)
}
if actualValue, ok := stack.Pop(); actualValue != nil || ok {
t.Errorf("Got %v expected %v", actualValue, nil)
}
if actualValue := stack.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
}

@ -20,6 +20,7 @@ with this distribution for more information.
// Used by TreeSet and TreeMap.
// Structure is not thread safe.
// References: http://en.wikipedia.org/wiki/Red%E2%80%93black_tree
package redblacktree
import (
@ -103,7 +104,7 @@ func (tree *Tree) Put(key interface{}, value interface{}) {
// Searches the node in the tree by key and returns its value or nil if key is not found in tree.
// Second return parameter is true if key was found, otherwise false.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *Tree) Get(key interface{}) (interface{}, bool) {
func (tree *Tree) Get(key interface{}) (value interface{}, found bool) {
node := tree.lookup(key)
if node != nil {
return node.value, true
@ -144,8 +145,8 @@ func (tree *Tree) Remove(key interface{}) {
}
// Returns true if tree does not contain any nodes
func (tree *Tree) IsEmpty() bool {
return tree.root == nil
func (tree *Tree) Empty() bool {
return tree.size == 0
}
// Returns number of nodes in the tree.
@ -155,7 +156,7 @@ func (tree *Tree) Size() int {
func (tree *Tree) String() string {
str := "RedBlackTree\n"
if !tree.IsEmpty() {
if !tree.Empty() {
output(tree.root, "", true, &str)
}
return str

Loading…
Cancel
Save