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++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
随机推荐
- Jupyter Notebooks的安装和使用介绍
最近又开始重新学习Python,学习中使用到了一款编辑器Jupyter Notebooks ,非常想安利给初学python的同学.注:本文内容仅针对windows环境下安装和配置Jupyter Not ...
- Memcached 基本语法 记录
set 命令:命令将value数值存储在指定的key中: set key flags exptime bytes [noreply] value key:键值 key-value 结构中的 key,用 ...
- 非法指令(Illegal Instruction)问题定位
关键词:Illegal Instruction.SIGILL等. 进程在运行过程中会收到SIGILL信号,此类错误是由操作系统发送给进程的. SIGILL是某个进程中的某一句不能被CPU识别指令,这些 ...
- Linux防火墙firewall和iptables的使用
防火墙是整个数据包进入主机前的第一道关卡. Linux中有两种防火墙软件,ConterOS 7.0以上使用的是 firewall,ConterOS 7.0以下使用的是 iptables,本文将分别介绍 ...
- 005 C/C++ 数据类型_void
1.void的字面意思是'无类型'.void * 是无类型指针,void * 可以指向任何类型的数据. 2.数据类型的分装: int InitHardEnv(void ** handle); 典型的内 ...
- Codeforces Round #588 (Div. 2)
传送门 A. Dawid and Bags of Candies 乱搞. Code #include <bits/stdc++.h> #define MP make_pair #defin ...
- C++ class 内的 () 重载示例
#include <iostream> // overloading "operator () " outside class //////////////////// ...
- CF414D Mashmokh and Water Tanks
CF414D Mashmokh and Water Tanks 洛谷评测传送门 题目描述 Mashmokh is playing a new game. In the beginning he has ...
- Windows10 下利用Hyper-V安装CentOS系统
开启Windows10的Hyper-v功能(需要重启电脑) 控制面板→程序→启用或关闭Windows功能→打开Hyper-v→确定 创建虚拟机 在Windows管理工具中找到Hyper-v管理器并双击 ...
- C语言中,嵌套的if语句的一些经验...
double f(double x){double result;if(x<0)result=-x+sin(x);else if(x==0)result=0; else if(10> ...