队列和堆栈不一样的地方在于进出顺序:

堆栈是后进先出,

队列是先进先出。

QueueLinkedList.go
package QueueLinkedList

type Node struct {
	data int
	next *Node
}

type Queue struct {
	rear *Node
}

func (list *Queue) Enqueue(i int) {
	data := &Node{data: i}
	if list.rear != nil {
		data.next = list.rear
	}
	list.rear = data
}

func (list *Queue) Dequeue() (int, bool) {
	if list.rear == nil {
		return 0, false
	}

	if list.rear.next == nil {
		i := list.rear.data
		list.rear = nil
		return i, true
	}
	current := list.rear
	for {
		if current.next.next == nil {
			i := current.next.data
			current.next = nil
			return i, true
		}
		current = current.next
	}
}

func (list *Queue) Peek() (int, bool) {
	if list.rear == nil {
		return 0, false
	}
	return list.rear.data, true
}

func (list *Queue) Get() []int {
	var items []int
	current := list.rear
	for current != nil {
		items = append(items, current.data)
		current = current.next
	}
	return items
}

func (list *Queue) IsEmpty() bool {
	return list.rear == nil
}

func (list *Queue) Empty() {
	list.rear = nil
}

  

QueueLinkedList_test.go
package QueueLinkedList

import (
	"fmt"
	"math/rand"
	"testing"
	"time"
)

func TestQueueLinkedList(t *testing.T) {
	random := rand.New(rand.NewSource(time.Now().UnixNano()))
	headNode := &Node{
		data: random.Intn(100),
		next: nil,
	}
	queue := &Queue{
		rear: headNode,
	}
	fmt.Println(queue.Get())

	randNumber := random.Intn(100)
	queue.Enqueue(randNumber)
	queue.Enqueue(random.Intn(100))
	queue.Enqueue(random.Intn(100))
	fmt.Println(queue.Get())
	queue.Dequeue()
	fmt.Println(queue.Get())
	retResult, retBool := queue.Peek()
	if retBool == true {
		fmt.Println(retResult)
	}
	queue.Empty()

	if queue.IsEmpty() == false {
		t.Fail()
	}

}

  

输出:
D:/Go/bin/go.exe test -v [D:/go-project/src/QueueLinkedList]
=== RUN   TestQueueLinkedList
[68]
[12 49 69 68]
[12 49 69]
12
--- PASS: TestQueueLinkedList (0.00s)
PASS
ok  	QueueLinkedList	2.177s
成功: 进程退出代码 0.

  

golang数据结构和算法之QueueLinkedList链表队列的更多相关文章

  1. golang数据结构和算法之CircularBuffer环形缓冲队列

    慢慢练语法和思路, 想说的都在代码及注释里. CircularBuffer package CircularBuffer const arraySize = 10 type CircularBuffe ...

  2. golang数据结构和算法之StackLinkedList链表堆栈

    会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...

  3. golang数据结构和算法之LinkedList链表

    差不多自己看懂了,可以自己写测试了.:) LinkedList.go package LinkedList //"fmt" type Node struct { data int ...

  4. JavaScript 版数据结构与算法(二)队列

    今天,我们要讲的是数据结构与算法中的队列. 队列简介 队列是什么?队列是一种先进先出(FIFO)的数据结构.队列有什么用呢?队列通常用来描述算法或生活中的一些先进先出的场景,比如: 在图的广度优先遍历 ...

  5. 数据结构和算法 – 3.堆栈和队列

    1.栈的实现   后进先出     自己实现栈的代码 using System; using System.Collections.Generic; using System.Linq; using ...

  6. JS数据结构及算法(二) 队列

    队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...

  7. golang数据结构和算法之BinarySearch二分查找法

    基础语法差不多了, 就需要系统的撸一下数据结构和算法了. 没找到合适的书, 就参考github项目: https://github.com/floyernick/Data-Structures-and ...

  8. java:数据结构复习(三)链表队列

    @TOC 和栈一样,队列也是表,但是使用队列的特点是先进先出. 队列模型 队列的基本操作是入队,它是在表的末端插入一个元素,和出队,它是删除在表开头的一个元素 graph LR A[<kbd&g ...

  9. 用JS描述的数据结构及算法表示——栈和队列(基础版)

    前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...

随机推荐

  1. scp跨服务器拷贝,后台运行

    [转载]原文:https://blog.csdn.net/u013091013/article/details/68941250 通常情况下,我门在同一台服务器拷贝数据最常用的命令便是cp,如果要在不 ...

  2. SQL语句添加表外键

    ALTER TABLE 表名 ADD CONSTRAINT FK_ID FOREIGN KEY(外键列) REFERENCES 外键表(外键表主键)

  3. postman---postman导出python脚本

    前面一直写关于postman的一些文章,大家现在都应该简单了解,其实postman还有许多的功能,这个要大家一点点的挖掘出来了,安静在给大家分享一个关于postman导出python脚本 Postma ...

  4. 第6次作业--static关键字、对象

    题目1:编写一个类Computer,类中含有一个求n的阶乘的方法.将该类打包,并在另一包中的Java文件App.java中引入包,在主类中定义Computer类的对象,调用求n的阶乘的方法(n值由参数 ...

  5. maven clean插件使用进阶

    maven clean插件使用进阶 参考 Maven clean 插件 Maven删除外部文件 查看命令帮助 mvn clean:help mvn clean:help -Ddetail=true - ...

  6. Windows许可证 即将过期

    最近打开电脑,系统总是自动弹出Windows许可证即将过期的弹窗,现在总结方法如下. 命令都是在运行窗口输入的打开方式:win+R组合键或者右键点击win10开始菜单,点击“运行”查看系统版本:win ...

  7. 一文学会 TypeScript 的 82% 常用知识点(上)

    前端专栏 2019-11-22 19:17:55 对于前端从业者来说,TypeScript(以下简称 TS)已经不算是新技术. Vue3 的源码基于 TS 编写, Angular 项目默认支持 TS ...

  8. pytest框架之rerunfailures失败重运行机制

    web自动化测试中,稳定性在整个测试运行中都至关重要,但不能保证测试脚本或者测试环境一直都能够稳定,它牵扯到诸多因素,在这里就不赘述,pytest框架相较于unittest的一大优势就在于拥有用例失败 ...

  9. oracle视图和索引

    视图和索引 视图 视图的作用 控制数据访问.简化查询.避免重复访问相同的数据 视图的优点 限制用户只能通过视图检索数据,用户看不到底层基表 注意事项 视图可以理解为临时表,会随着真实表的数据变化而自动 ...

  10. Seven Kinds of Testers - 七种类型的测试

    最近读了James大叔的一篇总结Tester类型的文章,获益良多.原文叫做Seven Kinds of Testers(链接:http://www.satisfice.com/blog/archive ...