队列的实现

//采用切片作为底层结构
package main 
import (
	"reflect"
	"fmt"
)
type Queue struct {
	Values []interface{}
	Type reflect.Type
}
//初始化一个队列
func NewQueue(valueType reflect.Type)*Queue{
	return &Queue{Values:make([]interface{},0),Type:valueType}
}
//判断是否符合队列类型
func (q *Queue) CheckValue(data interface{}) bool{
	if data == nil || reflect.TypeOf(data) != q.Type {
		return false
	}
	return true
}
//是否为空
func (q *Queue) Empty() bool {
	if len(q.Values) == 0 {
		return true
	}
	return false
}
//大小
func (q *Queue) Size() int {
	return len(q.Values)
}
//push
func (q *Queue) Push(data interface {}) bool {
	if !q.CheckValue(data){
		return false
	}
	q.Values = append(q.Values,data)
	return true
}
//pop 
func (q *Queue) Pop() interface{} {
	if q.Empty(){
		return nil
	}
	ret := q.Values[0]
	q.Values = q.Values[1:]
	return ret
}
func main() {
	queue := NewQueue(reflect.TypeOf(1))
	queue.Push(1)
	queue.Push(2)
	queue.Push(3)
	queue.Push(4)
	queue.Push(5)
	fmt.Println(queue.Values)
	fmt.Println(queue.Empty())
}
//也可采用container/list作为底层结构,更为简洁
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值