队列(Queue):First In Fist Out(FIFO)即先进先出
队列的概念:队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
package main
import(
"fmt"
)
//===================队列(基于顺序存储结构实现的)===========
/*
* 队列(基于顺序存储结构实现的)
* FIFO(First In Fist Out)
* ADT Queue
* Data
* 同线性表。元素具有相同的类型,相邻的元素具有前驱和后继。
* Operation
* InitQueue(*Q) :初始化操作,建立一个空的队列Q
* DestroyQueue(*Q): 若队列Q存在,则销毁它
* ClearQueue(*Q): 将队列Q存清空
* QueueEmpty(*Q): 若队列为空返回true,否则返回false
* GetHead(*Q,*e): 若队列Q存在有非空,用e返回队列Q的队头元素.
* EnQueue(*Q,e): 若队列存在,插入新元素e到队列Q中并成为队尾元素.
* DeQueu(*Q,*e): 删除队列Q中队头元素,并用e返回
* QueueLength(*Q): 返回队列Q的元素个数
* endADT
*/
const MAXSIZE = 5
type Queue struct{
qList []int //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)
//DestroyQueue(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++{
}
}
//===================队列(基于链式存储结构实现的)===========
//稍作了一些简化,去掉了头(front)和尾(rear)属性
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())
}
队列基于顺序存储结构和链式存储结构的代码实现(golang)
最新推荐文章于 2022-11-09 14:59:42 发布