golang数据结构和算法之LinkedList链表
差不多自己看懂了,可以自己写测试了。:)
LinkedList.go
package LinkedList
//"fmt"
type Node struct {
data int
next *Node
}
type LinkedList struct {
head *Node
}
func (list *LinkedList) InsertFirst(i int) {
data := &Node{data: i}
if list.head != nil {
data.next = list.head
}
list.head = data
}
func (list *LinkedList) InsertLast(i int) {
data := &Node{data: i}
if list.head == nil {
list.head = data
return
}
current := list.head
for current.next != nil {
current = current.next
}
current.next = data
}
func (list *LinkedList) RemoveByValue(i int) bool {
if list.head == nil {
return false
}
if list.head.data == i {
list.head = list.head.next
return true
}
current := list.head
for current.next != nil {
if current.next.data == i {
current.next = current.next.next
return true
}
current = current.next
}
return false
}
func (list *LinkedList) RemoveByIndex(i int) bool {
if list.head == nil {
return false
}
if i < 0 {
return false
}
if i == 0 {
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
}
current.next = current.next.next
return true
}
func (list *LinkedList) 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 *LinkedList) GetFirst() (int, bool) {
if list.head == nil {
return 0, false
}
return list.head.data, true
}
func (list *LinkedList) 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 *LinkedList) GetSize() int {
count := 0
current := list.head
for current != nil {
count += 1
current = current.next
}
return count
}
func (list *LinkedList) getItems() []int {
var items []int
current := list.head
for current != nil {
items = append(items, current.data)
current = current.next
}
return items
}
LinkedList_test.go
package LinkedList
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestLinkedList(t *testing.T) {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
headNode := &Node{
data: random.Intn(100),
next: nil,
}
list := &LinkedList{
head: headNode,
}
fmt.Println(list.getItems())
list.InsertFirst(random.Intn(100))
fmt.Println(list.getItems())
list.InsertLast(random.Intn(100))
fmt.Println(list.getItems())
randNumber := random.Intn(100)
list.InsertFirst(randNumber)
fmt.Println(list.getItems())
list.InsertLast(random.Intn(100))
fmt.Println(list.getItems())
list.InsertFirst(random.Intn(100))
fmt.Println(list.getItems())
if list.SearchValue(randNumber) == false {
t.Fail()
}
list.RemoveByValue(randNumber)
if list.SearchValue(randNumber) == true {
t.Fail()
}
fmt.Println(list.getItems())
fmt.Println(list.RemoveByIndex(1))
fmt.Println(list.getItems())
fmt.Println(list.GetFirst())
fmt.Println(list.GetLast())
fmt.Println(list.GetSize())
}

golang数据结构和算法之LinkedList链表的更多相关文章
- golang数据结构和算法之QueueLinkedList链表队列
队列和堆栈不一样的地方在于进出顺序: 堆栈是后进先出, 队列是先进先出. QueueLinkedList.go package QueueLinkedList type Node struct { d ...
- golang数据结构和算法之StackLinkedList链表堆栈
会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...
- 每周一练 之 数据结构与算法(LinkedList)
这是第三周的练习题,原本应该先发第二周的,因为周末的时候,我的母亲大人来看望她的宝贝儿子,哈哈,我得带她看看厦门这座美丽的城市呀. 这两天我抓紧整理下第二周的题目和答案,下面我把之前的也列出来: 1. ...
- golang数据结构和算法之BinarySearch二分查找法
基础语法差不多了, 就需要系统的撸一下数据结构和算法了. 没找到合适的书, 就参考github项目: https://github.com/floyernick/Data-Structures-and ...
- Android版数据结构与算法(三):基于链表的实现LinkedList源码彻底分析
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. LinkedList 是一个双向链表.它可以被当作堆栈.队列或双端队列进行操作.LinkedList相对于ArrayList来说,添加,删除元素效 ...
- java数据结构和算法04(链表)
前面我们看了数组,栈和队列,大概就会这些数据结构有了一些基本的认识,首先回顾一下之前的东西: 在数组中,其实是分为有序数组和无序数组,我简单实现了无序数组,为什么呢?因为有序数组的实现就是将无序数组进 ...
- 数据结构与算法JavaScript描述——链表
1.数组的缺点 数组不总是组织数据的最佳数据结构,原因如下. 在很多编程语言中,数组的长度是固定的,所以当数组已被数据填满时,再要加入新的元素就会非常困难. 在数组中,添加和删除元素也很麻烦,因为需要 ...
- 数据结构与算法(c++)——反转链表
算法概述:要求实现将一条单向链表反转并考虑时间复杂度. 算法分析: 数组法(略): 将列表元素逐个保存进数组,之后再逆向重建列表 点评:实现逻辑最简单,需要额外的内存开销. 移动指针: 通过三个指针逐 ...
- 数据结构和算法之单向链表二:获取倒数第K个节点
我们在做算法的时候或多或少都会遇到这样的问题,那就是我们需要获取某一个数据集的倒数或者正数第几个数据.那么今天我们来看一下这个问题,怎么去获取倒数第K个节点.我们拿到这个问题的时候自然而然会想到我们让 ...
随机推荐
- 失败的一天(找bug)
前天早上来到实验室,准备抓紧时间写写代码,毕竟第二天就是组会了.点了一下鼠标,发现显示屏无法唤醒,然后就准备强制关机再开机(我一般不关机,以前遇到过几次无法唤醒),低头发现主机不亮,然后按了开关也不亮 ...
- VMWare ESX server安装
和装普通虚拟机是一样的 需要添加两块网卡. F11同意协议 直接回车即可‘ 选择字符集 输入密码 按F11安装系统 按回车键重启 按F2 配置网络管理 启用两块网卡,按回车确定 配置IPV4地址 用空 ...
- redhat7.7(centOS7)安装ORACLE 11g出坑教程及问题总结与解决
写在前面: 环境建议:VM 15.5,因为15.5修复了诸多bug,可以在安装过程中省去不少麻烦 添加新的虚拟机安装redhat7.7 准备redhat7.7的安装包...百度云不让上传噢噢噢噢,这里 ...
- R的环境系统
r环境概念 环境:用来具体存储对象的地方. 规则1:每一个对象都存储在一个环境当中. 规则2:每一个环境都与一个父环境相连接,构成一个分层的环境系统. 规则3:子环境与父环境的连接是单向的. pa ...
- 【python爬虫】cookie & session
一.什么是cookie? cookie是指网站为了鉴别用户身份,进行会话跟踪而存储在客户端本地的数据. 二.什么是session? 本来的含义是指有始有终的一些列动作,而在web中,session对象 ...
- STM32 F4xx Fault 异常错误定位指南
STM32 F407 采用 Cortex-M4 的内核,该内核的 Fault 异常可以捕获非法的内存访问和非法的编程行为.Fault异常能够检测到以下几类非法行为: 总线 Fault: 在取址.数据读 ...
- 牛客网sql刷题解析-完结
查找最晚入职员工的所有信息 解题步骤: 题目:查询最晚入职员工的所有信息 目标:查询员工的所有信息 筛选条件:最晚入职 答案: SELECT *--查询所有信息就用* ...
- IT兄弟连 Java语法教程 流程控制语句 控制循环结构1
Java语言没有提供goto语句来控制程序的跳转,这种做法提高了程序流程控制的可读性,但降低了程序流程控制的灵活性.为了弥补这种不足,Java提供了continue和break来控制循环结构.除此之外 ...
- 【zabbix告警配置】zabbix服务配置邮件告警
一.安装邮件服务 在zabbix_server服务端安装邮件和邮件发送服务,这里可以参考我另一篇邮件服务部署文章:https://www.cnblogs.com/HeiDi-BoKe/p/118833 ...
- 鸟哥的Linux私房菜笔记第五章,文件权限与目录配置(二)
Linux目录配置的依据--FHS 因为利用Linux来开发产品的公司太多,例如,CentOS.Ubuntu.ReHat...,导致了配置文件存放的目录没有统一的标准.后来就有了FHS(Filesys ...