1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2022-05-20 17:17:31 +08:00
parent d4d68dc263
commit ed02794b04
11 changed files with 436 additions and 11 deletions

View File

@ -0,0 +1,2 @@
package collections

View File

@ -1,5 +1,6 @@
package collections
// 列表
type List[T any] interface {
Add(T)
Delete(T)
@ -7,6 +8,7 @@ type List[T any] interface {
ToSlice() []T
}
// 队列
type Queue[T any] interface {
Put(T)
Poll() T

View File

@ -40,21 +40,12 @@ func (q *ArrayQueue[T]) Poll() T {
// 队列最前面元素
v := q.array[0]
/* 直接原位移动,但缩容后继的空间不会被释放
for i := 1; i < queue.size; i++ {
// 从第一位开始进行数据移动
queue.array[i-1] = queue.array[i]
}
// 原数组缩容
queue.array = queue.array[0 : queue.size-1]
*/
// 创建新的数组,移动次数过多
newArray := make([]T, q.size-1)
for i := 1; i < q.size; i++ {
// 从老数组的第一位开始进行数据移动
newArray[i-1] = q.array[i]
copy(newArray, q.array[1:])
}
q.array = newArray
// 队中元素数量-1

View File

@ -15,6 +15,7 @@ type ArrayStack[T any] struct {
lock sync.Mutex // 为了并发安全使用的锁
}
// 初始化堆栈
func NewArrayStack[T any]() *ArrayStack[T] {
return &ArrayStack[T]{}
}