make linked list stack use our singly linked list

pull/1/head
Emir Pasic 11 years ago
parent 3aa1340d55
commit 67f317df5a

@ -283,7 +283,7 @@ type Interface interface {
This stack structure is based on a linked list, i.e. each previous element has a point to the next. This stack structure is based on a linked list, i.e. each previous element has a point to the next.
All operations are guaranted constant time performance. All operations are guaranted constant time performance, except _Values()_, which is as always of linear time performance.
```go ```go
package main package main

@ -24,7 +24,7 @@ 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. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
// Implementation of stack using linked list. // Implementation of stack backed by our singly linked list.
// Used by red-black tree during in-order traversal. // Used by red-black tree during in-order traversal.
// Structure is not thread safe. // Structure is not thread safe.
// References: http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29 // References: http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29
@ -33,6 +33,7 @@ package linkedliststack
import ( import (
"fmt" "fmt"
"github.com/emirpasic/gods/lists/singlylinkedlist"
"github.com/emirpasic/gods/stacks" "github.com/emirpasic/gods/stacks"
"strings" "strings"
) )
@ -42,78 +43,57 @@ func assertInterfaceImplementation() {
} }
type Stack struct { type Stack struct {
top *element list *singlylinkedlist.List
size int
}
type element struct {
value interface{}
next *element
} }
// Instantiates a new empty stack // Instantiates a new empty stack
func New() *Stack { func New() *Stack {
return &Stack{} return &Stack{list: &singlylinkedlist.List{}}
} }
// Pushes a value onto the top of the stack // Pushes a value onto the top of the stack
func (stack *Stack) Push(value interface{}) { func (stack *Stack) Push(value interface{}) {
stack.top = &element{value, stack.top} stack.list.Prepend(value)
stack.size += 1
} }
// Pops (removes) top element on stack and returns it, or nil if stack is empty. // 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. // Second return parameter is true, unless the stack was empty and there was nothing to pop.
func (stack *Stack) Pop() (value interface{}, ok bool) { func (stack *Stack) Pop() (value interface{}, ok bool) {
if stack.size > 0 { value, ok = stack.list.Get(0)
value, stack.top = stack.top.value, stack.top.next stack.list.Remove(0)
stack.size -= 1 return
return value, true
}
return nil, false
} }
// Returns top element on the stack without removing it, or nil if stack is empty. // 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. // Second return parameter is true, unless the stack was empty and there was nothing to peek.
func (stack *Stack) Peek() (value interface{}, ok bool) { func (stack *Stack) Peek() (value interface{}, ok bool) {
if stack.size > 0 { return stack.list.Get(0)
return stack.top.value, true
}
return nil, false
} }
// Returns true if stack does not contain any elements. // Returns true if stack does not contain any elements.
func (stack *Stack) Empty() bool { func (stack *Stack) Empty() bool {
return stack.size == 0 return stack.list.Empty()
} }
// Returns number of elements within the stack. // Returns number of elements within the stack.
func (stack *Stack) Size() int { func (stack *Stack) Size() int {
return stack.size return stack.list.Size()
} }
// Removes all elements from the stack. // Removes all elements from the stack.
func (stack *Stack) Clear() { func (stack *Stack) Clear() {
stack.top = nil stack.list.Clear()
stack.size = 0
} }
// Returns all elements in the stack (LIFO order). // Returns all elements in the stack (LIFO order).
func (stack *Stack) Values() []interface{} { func (stack *Stack) Values() []interface{} {
elements := make([]interface{}, stack.size, stack.size) return stack.list.Values()
element := stack.top
for index := 0; element != nil; index++ {
elements[index] = element.value
element = element.next
}
return elements
} }
func (stack *Stack) String() string { func (stack *Stack) String() string {
str := "LinkedListStack\n" str := "LinkedListStack\n"
values := []string{} values := []string{}
for _, value := range stack.Values() { for _, value := range stack.list.Values() {
values = append(values, fmt.Sprintf("%v", value)) values = append(values, fmt.Sprintf("%v", value))
} }
str += strings.Join(values, ", ") str += strings.Join(values, ", ")

Loading…
Cancel
Save