-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from 246859/feat-linked_stack
Feat linked stack
- Loading branch information
Showing
5 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
|
||
- stack | ||
- ArrayStack ✔️ | ||
- LinkedStack | ||
- LinkedStack ✔️ | ||
|
||
- heap | ||
- BinaryHeap ✔️ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,6 @@ type Stack[T any] interface { | |
Peek() (T, bool) | ||
|
||
containers.Container[T] | ||
|
||
containers.IndexIterable[T] | ||
} |