From ce767c333de5f6d25ca8e3740ddf1faed46728e8 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Thu, 5 Mar 2015 22:37:25 +0100 Subject: [PATCH] add Clear() method to stack interface --- stacks/arraystack/arraystack.go | 6 ++++++ stacks/linkedliststack/linkedliststack.go | 6 ++++++ stacks/stacks.go | 1 + 3 files changed, 13 insertions(+) diff --git a/stacks/arraystack/arraystack.go b/stacks/arraystack/arraystack.go index 8034731..63107f4 100644 --- a/stacks/arraystack/arraystack.go +++ b/stacks/arraystack/arraystack.go @@ -88,6 +88,12 @@ func (stack *Stack) Size() int { return stack.top + 1 } +// Removes all elements from the stack. +func (stack *Stack) Clear() { + stack.top = -1 + stack.items = []interface{}{} +} + func (stack *Stack) String() string { str := "ArrayStack\n" values := []string{} diff --git a/stacks/linkedliststack/linkedliststack.go b/stacks/linkedliststack/linkedliststack.go index c1571d9..f3b1762 100644 --- a/stacks/linkedliststack/linkedliststack.go +++ b/stacks/linkedliststack/linkedliststack.go @@ -84,6 +84,12 @@ func (stack *Stack) Size() int { return stack.size } +// Removes all elements from the stack. +func (stack *Stack) Clear() { + stack.top = nil + stack.size = 0 +} + func (stack *Stack) String() string { str := "LinkedListStack\n" element := stack.top diff --git a/stacks/stacks.go b/stacks/stacks.go index d55d03e..2c78aea 100644 --- a/stacks/stacks.go +++ b/stacks/stacks.go @@ -24,4 +24,5 @@ type Interface interface { Peek() (value interface{}, ok bool) Empty() bool Size() int + Clear() }