队列:
// 队列
package Lstruct
import "errors"
type Queue []interface {}
func (q *Queue) Push(x interface{}) {
*q = append(*q, x)
}
func (q *Queue) Pop() (interface{}, error) {
if len(*q) == 0 {
return nil, errors.New("Can't pop an empty queue")
}
top := (*q)[0]
*q = (*q)[1:]
return top, nil
}
func (q Queue) Top() (interface{}, error){
if len(q) == 0 {
return nil, errors.New("Can't top an empty queue")
}
return q[0], nil
}
func (q Queue) IsEmpty() bool {
return (len(q) == 0)
}
func (q Queue) Len()int{
return len(q)
}
堆栈:
//堆栈
package Lstruct
import "errors"
type Stack []interface{}
func (s *Stack) Push (x interface{}){
*s = append(*s, x)
}
func (s *Stack) Pop() (interface{}, error){
temp := *s
if len(temp) == 0{
return nil, errors.New("Can't pop an empty stack")
}
x := temp[len(temp) - 1]
*s = temp[:len(temp) - 1]
return x, nil
}
func (s Stack) Top() (interface{}, error){
if len(s) == 0 {
return nil, errors.New("Can't top en empty stack")
}
return s[len(s) - 1], nil
}
func (s Stack) Len()int{
return len(s)
}
func (s Stack) IsEmpty() bool {
return (len(s) == 0)
}
后续会持续完善各种数据结构和主流算法。