双向链表比起单向链表,

多了一个向前指向的指针,

所以在增删查改时,要同时照顾到两个指针的指向。

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双向链表的更多相关文章

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

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

  2. 数据结构与算法-python描述-双向链表

    # coding:utf-8 # 双向链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历链表 # add(item) 链表头部添加 # ...

  3. golang数据结构和算法之QueueLinkedList链表队列

    队列和堆栈不一样的地方在于进出顺序: 堆栈是后进先出, 队列是先进先出. QueueLinkedList.go package QueueLinkedList type Node struct { d ...

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

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

  5. golang数据结构和算法之StackArray数组堆栈

    用数组实现的堆栈, 另一种,是用链表实现的堆栈, 在各种不同的编程语言上, 实现都是类似的. StackArray.go package StackArray //基于数组实现的堆栈 const ar ...

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

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

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

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

  8. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

  9. 数据结构和算法(Golang实现)(15)常见数据结构-列表

    列表 一.列表 List 我们又经常听到列表 List数据结构,其实这只是更宏观的统称,表示存放数据的队列. 列表List:存放数据,数据按顺序排列,可以依次入队和出队,有序号关系,可以取出某序号的数 ...

随机推荐

  1. MySQL数据库:基本操作及增删改查语句

    基本语法&&操作语句 create(创建) alter(更新) drop(删除) 一次性删除一个表中所有的数据 包括日志 truncate table 表名; 选中或者使用该数据库 说 ...

  2. TensorFlow从1到2(十二)生成对抗网络GAN和图片自动生成

    生成对抗网络的概念 上一篇中介绍的VAE自动编码器具备了一定程度的创造特征,能够"无中生有"的由一组随机数向量生成手写字符的图片. 这个"创造能力"我们在模型中 ...

  3. ssm-restful风格

    码云 https://gitee.com/MarkPolaris/ssm-test02

  4. Codeforces Round #594 (Div. 1) D2. The World Is Just a Programming Task (Hard Version) 括号序列 思维

    D2. The World Is Just a Programming Task (Hard Version) This is a harder version of the problem. In ...

  5. dicom(dcm)文件批量Study Instance UID打包整理工具

    一款可以自动识别原始dicom文件Study Instance UID的工具. 如果你有一堆混乱不堪的dcm文件,这个小工具能帮助你将这些无序的dicom文件按照Study Instance UID压 ...

  6. Java连载45-继承举例、方法覆盖

    一.Java语言中假设一个类没有显式的继承任何类,那么该类默认继承Java SE库中提供的java.lang.Object类 1.快捷键:Ctrl + shift + T:可以在Myeclipse中查 ...

  7. IT兄弟连 Java语法教程 注释与编码规范

    在程序代码中适当地添加注释可以提高程序的可读性和可维护性.好的编码规范可以使程序更易阅读和理解.下面将介绍Java中的集中代码注释以及应该注意的编码规范. 代码注释 通过在程序代码中添加注释可提高程序 ...

  8. Linux 下编写一个 PHP 扩展

        假设需求 开发一个叫做 helloWord 的扩展. 扩展里有一个函数,helloWord(). echo helloWord('Tom'); //返回:Hello World: Tom 本地 ...

  9. CodeForces 200D Programming Language

    Recently, Valery have come across an entirely new programming language. Most of all the language att ...

  10. Java Serializable:明明就一个空的接口嘛

    对于 Java 的序列化,我一直停留在最浅显的认知上——把那个要序列化的类实现 Serializbale 接口就可以了.我不愿意做更深入的研究,因为会用就行了嘛. 但随着时间的推移,见到 Serial ...