队列基于顺序存储结构和链式存储结构的代码实现(golang)

队列(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())

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值