Golang 구현 대기열 및 스택
                                            
 1332 단어  Golang
                    
//  
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)
}github 주소:https://github.com/golibec/Lstruct.git
후속적으로 각종 데이터 구조와 주류 알고리즘을 지속적으로 보완할 것이다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Golang 구현 대기열 및 스택대기열: 스택: github 주소:https://github.com/golibec/Lstruct.git 후속적으로 각종 데이터 구조와 주류 알고리즘을 지속적으로 보완할 것이다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.