队列(Queue):First In Fist Out(FIFO)即先进先出
队列的概念:队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
package main
import(
"fmt"
)
const MAXSIZE = 5
type Queue struct{
qList []int
front int
rear int
}
func InitQueue(Q *Queue){
Q.qList = make([]int,MAXSIZE)
Q.front = 0
Q.rear = 0
}
func DestroyQueue(Q *Queue){
Q.qList = nil
Q.front = 0
Q.rear = 0
}
func Clear(Q *Queue){
Q.qList = make([]int,MAXSIZE)
Q.front = 0
Q.rear = 0
}
func QueueEmpty(Q *Queue) bool{
if Q.qList == nil || len(Q.qList) == 0 {
return true
}
return false
}
func GetHead(Q *Queue,e *int){
if Q.front >= len(Q.qList) || Q.qList == nil{
fmt.Println("queue is emtpy or is nill")
return
}
*e = Q.qList[Q.front]
}
func DeQueue(Q *Queue,e *int){
if Q.qList == nil{
fmt.Println("queue is emtpy or is nill")
return
}
*e = Q.qList[Q.front]
Q.qList[Q.front] = 0
Q.front++
if Q.front == len(Q.qList){
Q.front = 0
Q.rear = 0
}
}
func EnQueue(Q *Queue,e int){
if Q.rear == len(Q.qList) || Q.qList == nil{
fmt.Println("queue is full or is nill")
return
}
Q.qList[Q.rear] = e
Q.rear++
}
func QueueLength(Q *Queue)int{
return Q.rear
}
func main(){
var q *Queue = &Queue{}
fmt.Printf("q:%#v\n",q)
InitQueue(q)
fmt.Printf("q:%#v\n",q)
for i:=0;i <5;i++{
EnQueue(q,(i+1)*2)
}
fmt.Printf("queue size:%d\n",QueueLength(q))
fmt.Printf("q:%#v\n",q)
EnQueue(q,100)
fmt.Println("\n\n")
var e *int=new(int)
GetHead(q,e)
fmt.Printf("e vlaue:%v\n",*e)
fmt.Printf("q:%#v\n",q)
fmt.Println("\n\n")
for i:=0; i < 5; i++{
DeQueue(q,e)
fmt.Printf("e:%v\n",*e)
}
fmt.Printf("queue size:%d\n",QueueLength(q))
fmt.Printf("q:%#v\n",q)
for i:=0; i < 5;i++{
}
}
linkQueue.go
package main
import (
"fmt"
)
type QueueNode struct {
Value interface{}
Next *QueueNode
}
type LinkQueue struct {
Size int
Queue *QueueNode
}
func NewQueue() *LinkQueue {
return &LinkQueue{}
}
func (this *LinkQueue) Add(e interface{}) {
if this.Queue == nil {
node := &QueueNode{
Value: e,
}
this.Queue = node
} else {
node := this.Queue
for {
if node.Next != nil {
node = node.Next
} else {
break
}
}
newNode := &QueueNode{
Value: e,
}
node.Next = newNode
}
this.Size++
}
func (this *LinkQueue) Get() (e interface{}, err error) {
if this.Queue == nil {
return nil, fmt.Errorf("queue is nil")
}
e = this.Queue.Value
this.Queue = this.Queue.Next
this.Size--
return
}
func (this *LinkQueue) Head() (e interface{}, err error) {
if this.Queue == nil {
return nil, fmt.Errorf("queue is nil")
}
e = this.Queue.Value
return
}
func (this *LinkQueue) IsEmpty() bool {
if this.Queue == nil {
return true
}
return false
}
func (this *LinkQueue) Clear() (bool, error) {
if this.Queue == nil {
return false, fmt.Errorf("queue is nil")
}
this.Queue = nil
return true, nil
}
func (this *LinkQueue) GetSize() int {
return this.Size
}
func main() {
q := NewQueue()
fmt.Printf("queue:%#v, size:%v, is empty:%v\n", q, q.GetSize(), q.IsEmpty())
for i := 0; i < 10; i++ {
q.Add((i+1) * 2)
}
fmt.Printf("queue:%#v, size:%v, is empty:%v\n", q, q.GetSize(), q.IsEmpty())
head,err := q.Head()
fmt.Printf("head:%v, err:%v\n",head,err)
fmt.Println()
for i:=0; i< 10; i++{
value,err := q.Get()
fmt.Printf("value:%v, err:%v\n",value,err)
}
fmt.Printf("queue:%#v, size:%v, is empty:%v\n", q, q.GetSize(), q.IsEmpty())
fmt.Println()
fmt.Println()
for i:=10; i <15; i++{
q.Add(i)
}
fmt.Printf("queue:%#v, size:%v, is empty:%v\n", q, q.GetSize(), q.IsEmpty())
head,err = q.Head()
fmt.Printf("head:%v,err:%v\n",head,err)
for i:=0; i <6;i++{
value,err := q.Get()
fmt.Printf("value:%v,err:%v\n",value,err)
}
fmt.Printf("queue:%#v, size:%v, is empty:%v\n", q, q.GetSize(), q.IsEmpty())
}