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

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

@ -25,6 +25,7 @@ package linkedliststack
import ( import (
"fmt" "fmt"
"strings"
) )
type Stack struct { type Stack struct {
@ -68,10 +69,12 @@ func (stack *Stack) Peek() (value interface{}, ok bool) {
return nil, false return nil, false
} }
// 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.size == 0
} }
// Returns number of elements within the stack.
func (stack *Stack) Size() int { func (stack *Stack) Size() int {
return stack.size return stack.size
} }
@ -87,6 +90,6 @@ func (stack *Stack) String() string {
for i, j := 0, len(elementsValues)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(elementsValues)-1; i < j; i, j = i+1, j-1 {
elementsValues[i], elementsValues[j] = elementsValues[j], elementsValues[i] elementsValues[i], elementsValues[j] = elementsValues[j], elementsValues[i]
} }
str += fmt.Sprintf("#v", elementsValues) str += strings.Join(elementsValues, ", ")
return str return str
} }

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

Loading…
Cancel
Save