mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
create
This commit is contained in:
2
collections/linked_list.go
Normal file
2
collections/linked_list.go
Normal file
@ -0,0 +1,2 @@
|
||||
package collections
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -15,6 +15,7 @@ type ArrayStack[T any] struct {
|
||||
lock sync.Mutex // 为了并发安全使用的锁
|
||||
}
|
||||
|
||||
// 初始化堆栈
|
||||
func NewArrayStack[T any]() *ArrayStack[T] {
|
||||
return &ArrayStack[T]{}
|
||||
}
|
||||
|
Reference in New Issue
Block a user