Skip to content

Commit

Permalink
Merge pull request #8 from 246859/feat-linked_stack
Browse files Browse the repository at this point in the history
Feat linked stack
  • Loading branch information
246859 authored Dec 27, 2023
2 parents c720ac9 + f4c7c7b commit f755ac8
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 2 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

- stack
- ArrayStack ✔️
- LinkedStack
- LinkedStack ✔️

- heap
- BinaryHeap ✔️
Expand Down
2 changes: 1 addition & 1 deletion lists/linked_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (l *LinkedList[T]) String() string {
var b strings.Builder
b.WriteString("LinkedList[")
elems := l.Values()
for i, v := range l.Values() {
for i, v := range elems {
b.WriteString(fmt.Sprintf("%v", v))
if i != len(elems)-1 {
b.WriteByte(',')
Expand Down
68 changes: 68 additions & 0 deletions stacks/linked_stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package stacks

import (
"fmt"
"github.com/246859/containers"
"github.com/246859/containers/lists"
"strings"
)

var _ Stack[any] = (*LinkedStack[any])(nil)

func NewLinkedStack[T any]() *LinkedStack[T] {
return &LinkedStack[T]{
list: lists.NewLinkedList[T](),
}
}

// LinkedStack is the stack implemented by lists.LinkedList
type LinkedStack[T any] struct {
list *lists.LinkedList[T]
}

func (l *LinkedStack[T]) Push(t ...T) {
l.list.Add(t...)
}

func (l *LinkedStack[T]) Pop() (T, bool) {
val, found := l.list.Get(l.Size() - 1)
if !found {
return val, false
}
l.list.Remove(l.Size() - 1)
return val, true
}

func (l *LinkedStack[T]) Peek() (T, bool) {
return l.list.Get(l.Size() - 1)
}

func (l *LinkedStack[T]) Values() []T {
return l.list.Values()
}

func (l *LinkedStack[T]) Size() int {
return l.list.Size()
}

func (l *LinkedStack[T]) Clear() {
l.list.Clear()
}

func (l *LinkedStack[T]) Iterator() containers.IndexIterator[T] {
return l.list.Iterator()
}

func (l *LinkedStack[T]) String() string {
var b strings.Builder
b.WriteString("LinkedStack[")
elems := l.Values()
for i, v := range elems {
b.WriteString(fmt.Sprintf("%v", v))
if i != len(elems)-1 {
b.WriteByte(',')
}
}
b.WriteByte(']')
return b.String()
}
83 changes: 83 additions & 0 deletions stacks/linked_stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package stacks

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestLinkedStack_Push_Pop(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

stack := NewLinkedStack[int]()
stack.Push(data[:5]...)
stack.Push(data[5:]...)

for i := len(data) - 1; i >= 0; i-- {
d := data[i]
pop, b := stack.Pop()
assert.True(t, b)
assert.Equal(t, d, pop)
}
}

func TestLinkedStack_Push_Peek(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

stack := NewLinkedStack[int]()
stack.Push(data[:5]...)
stack.Push(data[5:]...)

for i := len(data) - 1; i >= 0; i-- {
d := data[i]
pop, b := stack.Peek()
assert.True(t, b)
assert.Equal(t, d, pop)

stack.Pop()
}

}

func TestLinkedStack_Iterator(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

stack := NewLinkedStack[int]()
stack.Push(data[:5]...)
stack.Push(data[5:]...)

it := stack.Iterator()
for ; it.Valid(); it.Next() {
assert.Equal(t, data[it.Index()], it.Value())
}

it.Reverse()
it.Rewind()

for ; it.Valid(); it.Next() {
assert.Equal(t, data[it.Index()], it.Value())
}
}

func TestLinkedStack_Values(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

stack := NewLinkedStack[int]()
stack.Push(data[:5]...)
stack.Push(data[5:]...)

values := stack.Values()

for i := 0; i < len(values); i++ {
assert.Equal(t, data[i], values[i])
}
}

func TestLinkedStack_Size(t *testing.T) {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

stack := NewLinkedStack[int]()
stack.Push(data[:5]...)
assert.Equal(t, 5, stack.Size())
stack.Push(data[5:]...)
assert.Equal(t, 10, stack.Size())
}
2 changes: 2 additions & 0 deletions stacks/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ type Stack[T any] interface {
Peek() (T, bool)

containers.Container[T]

containers.IndexIterable[T]
}

0 comments on commit f755ac8

Please sign in to comment.