more documentation added on linked list stack. String() method of linked list stack changed

pull/1/head
Emir Pasic 9 years ago
parent 2ac9c4d384
commit eff67601ed

@ -25,6 +25,7 @@ package linkedliststack
import (
"fmt"
"strings"
)
type Stack struct {
@ -68,10 +69,12 @@ func (stack *Stack) Peek() (value interface{}, ok bool) {
return nil, false
}
// Returns true if stack does not contain any elements.
func (stack *Stack) Empty() bool {
return stack.size == 0
}
// Returns number of elements within the stack.
func (stack *Stack) Size() int {
return stack.size
}
@ -81,12 +84,12 @@ func (stack *Stack) String() string {
element := stack.top
elementsValues := []string{}
for element != nil {
elementsValues = append(elementsValues, fmt.Sprintf("%v ", element.value))
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)
str += strings.Join(elementsValues, ", ")
return str
}

@ -19,6 +19,7 @@ with this distribution for more information.
package linkedliststack
import (
"fmt"
"testing"
)
@ -35,6 +36,8 @@ func TestLinkedListStack(t *testing.T) {
stack.Push(2)
stack.Push(3)
fmt.Println(stack)
if actualValue := stack.Empty(); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}

Loading…
Cancel
Save