golang数据结构和算法之DoublyLinkedList双向链表
双向链表比起单向链表,
多了一个向前指向的指针,
所以在增删查改时,要同时照顾到两个指针的指向。
DoublyLinkedList.go
package DoublyLinkedList
//双向链表
type Node struct {
data int
next *Node
prev *Node
}
type DoublyLinkedList struct {
head *Node
tail *Node
}
func (list *DoublyLinkedList) InsertFirst(i int) {
data := &Node{data: i}
if list.head != nil {
list.head.prev = data
data.next = list.head
}
list.head = data
}
func (list *DoublyLinkedList) InsertLast(i int) {
data := &Node{data: i}
if list.head == nil {
list.head = data
list.tail = data
return
}
if list.tail != nil {
list.tail.next = data
data.prev = list.tail
}
list.tail = data
}
func (list *DoublyLinkedList) RemoveByValue(i int) bool {
if list.head == nil {
return false
}
if list.head.data == i {
list.head = list.head.next
list.head.prev = nil
return true
}
if list.tail.data == i {
list.tail = list.tail.prev
list.tail.next = nil
return true
}
current := list.head
for current.next != nil {
if current.next.data == i {
if current.next.next != nil {
current.next.next.prev = current
}
current.next = current.next.next
return true
}
current = current.next
}
return false
}
func (list *DoublyLinkedList) RemoveByIndex(i int) bool {
if list.head == nil {
return false
}
if i < 0 {
return false
}
if i == 0 {
list.head.prev = nil
list.head = list.head.next
return true
}
current := list.head
for u := 1; u < i; u++ {
if current.next.next == nil {
return false
}
current = current.next
}
if current.next.next != nil {
current.next.next.prev = current
}
current.next = current.next.next
return true
}
func (list *DoublyLinkedList) SearchValue(i int) bool {
if list.head == nil {
return false
}
current := list.head
for current != nil {
if current.data == i {
return true
}
current = current.next
}
return false
}
func (list *DoublyLinkedList) GetFirst() (int, bool) {
if list.head == nil {
return 0, false
}
return list.head.data, true
}
func (list *DoublyLinkedList) GetLast() (int, bool) {
if list.head == nil {
return 0, false
}
current := list.head
for current.next != nil {
current = current.next
}
return current.data, true
}
func (list *DoublyLinkedList) GetSize() int {
count := 0
current := list.head
for current != nil {
count += 1
current = current.next
}
return count
}
func (list *DoublyLinkedList) GetItemFromStart() []int {
var items []int
current := list.head
for current != nil {
items = append(items, current.data)
current = current.next
}
return items
}
func (list *DoublyLinkedList) GetItemFromEnd() []int {
var items []int
current := list.tail
for current != nil {
items = append(items, current.data)
current = current.prev
}
return items
}
DoublyLinkedList_test.go
package DoublyLinkedList
//使用随机数作测试
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestDoublyLinkedList(t *testing.T) {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
headNode := &Node{
data: random.Intn(100),
next: nil,
prev: nil,
}
list := &DoublyLinkedList{
head: headNode,
tail: headNode,
}
fmt.Println(list.GetItemFromStart())
list.InsertFirst(random.Intn(100))
fmt.Println(list.GetItemFromStart())
list.InsertLast(random.Intn(100))
fmt.Println(list.GetItemFromStart())
randNumber := random.Intn(100)
list.InsertFirst(randNumber)
fmt.Println(list.GetItemFromStart())
list.InsertLast(random.Intn(100))
fmt.Println(list.GetItemFromStart())
list.InsertFirst(random.Intn(100))
fmt.Println(list.GetItemFromStart())
fmt.Println(list.GetItemFromEnd())
if list.SearchValue(randNumber) == false {
t.Fail()
}
list.RemoveByValue(randNumber)
if list.SearchValue(randNumber) == true {
t.Fail()
}
fmt.Println(list.GetFirst())
fmt.Println(list.GetLast())
fmt.Println(list.GetSize())
}

golang数据结构和算法之DoublyLinkedList双向链表的更多相关文章
- golang数据结构和算法之BinarySearch二分查找法
基础语法差不多了, 就需要系统的撸一下数据结构和算法了. 没找到合适的书, 就参考github项目: https://github.com/floyernick/Data-Structures-and ...
- 数据结构与算法-python描述-双向链表
# coding:utf-8 # 双向链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历链表 # add(item) 链表头部添加 # ...
- golang数据结构和算法之QueueLinkedList链表队列
队列和堆栈不一样的地方在于进出顺序: 堆栈是后进先出, 队列是先进先出. QueueLinkedList.go package QueueLinkedList type Node struct { d ...
- golang数据结构和算法之StackLinkedList链表堆栈
会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...
- golang数据结构和算法之StackArray数组堆栈
用数组实现的堆栈, 另一种,是用链表实现的堆栈, 在各种不同的编程语言上, 实现都是类似的. StackArray.go package StackArray //基于数组实现的堆栈 const ar ...
- golang数据结构和算法之LinkedList链表
差不多自己看懂了,可以自己写测试了.:) LinkedList.go package LinkedList //"fmt" type Node struct { data int ...
- golang数据结构和算法之CircularBuffer环形缓冲队列
慢慢练语法和思路, 想说的都在代码及注释里. CircularBuffer package CircularBuffer const arraySize = 10 type CircularBuffe ...
- 数据结构与算法 Big O 备忘录与现实
不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新. 算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...
- 数据结构和算法(Golang实现)(15)常见数据结构-列表
列表 一.列表 List 我们又经常听到列表 List数据结构,其实这只是更宏观的统称,表示存放数据的队列. 列表List:存放数据,数据按顺序排列,可以依次入队和出队,有序号关系,可以取出某序号的数 ...
随机推荐
- 利用phpqrcode二维码生成类库合成带logo的二维码并且用合成的二维码生成海报(二)
前期准备 引入phpqrcode类库(下载地址:https://download.csdn.net/download/weixin_37557729/11891240:支持彩色二维码的下载地址:htt ...
- Linux—vi/vim命令详解
如何在 vi 里搜索关键字 在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 "/" 然后输入你要查找的关键字敲回车就行了. 如果你要继续查找此关键字,敲字符 ...
- ping测试丢包率
测试环境:Centos 6.4 增加参数:-i 例如: #ping -i 0.01 172.16.3.1 则每隔0.01秒ping一次
- 使用docker-compose安装wordpress
一.建立应用的目录 mkdir my_wordpress cd my_wordpress 二.创建 docker-compose.yml touch docker-compose.yml;vi doc ...
- [转]java 通过反射获取类的全局变量、方法、构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.str; public class ZiFuChuan { ...
- JAVA学习路线,实战开发
Java基础课程内容包含:Java开发介绍.Java数组.Java面向对象.常用基础类.集合.IO流.多线程.异常.反射. 第一部分:Java开发介绍 1. DOS常用命令 2. JVM.JR ...
- Day14 - Python基础14 事件驱动模型、IO模型
本节内容: 1:事件驱动模型 2:IO模型前戏准备 3:4种IO模型 1:事件驱动模型 传统的编程是如下线性模式的: 开始--->代码块A--->代码块B--->代码块C---> ...
- 【oracle】表和索引建立在不用表空间原因
磁盘I/O竞争,要放在[真]的不同的磁盘上. Oracle强烈建议,任何一个应用程序的库表至少需要创建两个表空间,其中之一用于存储表数据,而另一个用于存储表索引数据.因为如果将表数据和索引数据放在一起 ...
- Java并发编程:Java实现多线程的几种方式
在Java中,多线程主要的实现方式有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService ...
- 【2019.10.7 CCF-CSP-2019模拟赛 T1】树上查询(tree)(思维)
思维 这道题应该算是一道思维题吧. 首先你要想到,既然这是一棵无根树,就要明智地选择根--以第一个黑点为根(不要像我一样习惯性以\(1\)号点为根,结果直到心态爆炸都没做出来). 想到这一点,这题就很 ...