golang数据结构和算法之QueueLinkedList链表队列
队列和堆栈不一样的地方在于进出顺序:
堆栈是后进先出,
队列是先进先出。
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链表队列的更多相关文章
- golang数据结构和算法之CircularBuffer环形缓冲队列
慢慢练语法和思路, 想说的都在代码及注释里. CircularBuffer package CircularBuffer const arraySize = 10 type CircularBuffe ...
- golang数据结构和算法之StackLinkedList链表堆栈
会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...
- golang数据结构和算法之LinkedList链表
差不多自己看懂了,可以自己写测试了.:) LinkedList.go package LinkedList //"fmt" type Node struct { data int ...
- JavaScript 版数据结构与算法(二)队列
今天,我们要讲的是数据结构与算法中的队列. 队列简介 队列是什么?队列是一种先进先出(FIFO)的数据结构.队列有什么用呢?队列通常用来描述算法或生活中的一些先进先出的场景,比如: 在图的广度优先遍历 ...
- 数据结构和算法 – 3.堆栈和队列
1.栈的实现 后进先出 自己实现栈的代码 using System; using System.Collections.Generic; using System.Linq; using ...
- JS数据结构及算法(二) 队列
队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...
- golang数据结构和算法之BinarySearch二分查找法
基础语法差不多了, 就需要系统的撸一下数据结构和算法了. 没找到合适的书, 就参考github项目: https://github.com/floyernick/Data-Structures-and ...
- java:数据结构复习(三)链表队列
@TOC 和栈一样,队列也是表,但是使用队列的特点是先进先出. 队列模型 队列的基本操作是入队,它是在表的末端插入一个元素,和出队,它是删除在表开头的一个元素 graph LR A[<kbd&g ...
- 用JS描述的数据结构及算法表示——栈和队列(基础版)
前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
随机推荐
- JavaScript 是如何运行的?
摘要: 理解JS执行原理. 原文:JavaScript 是如何运行的? 作者:hengg Fundebug经授权转载,版权归原作者所有. 什么是JavaScript? 我们来确认一下JavaScrip ...
- Python中机器学习-验证码识别-粗略总结
#验证码识别# 解决办法:将验证码切割成单个字符训练 遇到问题:验证码字符大小不一或重叠 对上述问题的解决:通过CNN(卷积神经网络)直接就是端到端不分割的识别方式 处理验证码:将图片二值化 输入验证 ...
- volatile简记
volatile指出变量随时可能发生变化,与该变量有关的运算,不要进行编译优化,以免出错,也就是在使用变量时必须从它的地址中重新读取.
- liunx用户环境初始化脚本
liunx用户环境初始化脚本 编写生成脚本基本格式,包括作者,联系方式,版本,时间,描述等 [root@magedu ~]# vim .vimrc set ignorecase set c ...
- How To Restore Rman Backups On A Different Node When The Directory Structures Are Different (Doc ID 419137.1)
How To Restore Rman Backups On A Different Node When The Directory Structures Are Different (Doc ID ...
- list集合优化,泛型的使用以及坼箱和装箱
1.list vector线程同步,线程安全arraylist的运行速度较快,因为没有使用线程 LlinkedList以链表结构存储数据 2.泛型泛型类 泛型接口 泛型方法 泛型的属性泛型的属性 属性 ...
- 201871010116-祁英红《面向对象程序设计(java)》第十二周学习总结
博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...
- mysql导出数据的几种形式-待更新
1.导出某个数据库的某张表,添加where条件 mysqldump -u [用户名] -p -h [ip地址] --default-character-set=utf8 [数据库名] [表名] - ...
- antd配置config-overrides.js文件
下载antd 包 npm install antd 下载依赖包(定义组件按需求打包) npm install react-app-rewired customize-cra babel-plugin- ...
- CSP-S 2019文澜中学游记(11.15~11.17)
前言 今年的\(CSP-S\),本以为自己的实力与去年的\(NOIP\)相比,能有较大的提升的. 没想到,菜是原罪,弱就是弱,依然逃脱不了被吊锤的命运. \(Nov\ 15th\):\(Day\ 0\ ...