zoukankan      html  css  js  c++  java
  • 随笔练习:单链表 --- golang

    package main
    
    import (
        "fmt"
    )
    
    type ListNode struct{
        data int
        next *ListNode
    }
    
    func NewListNode(data int) *ListNode{
        return &ListNode{data:data}
    }
    
    type List struct {
        lenght int
        root *ListNode
    }
    
    func (o *List)append(data int){
        node := NewListNode(data)
        if o.root == nil{
            o.root = node
            o.lenght++
            return
        }else {
            cur := o.root
            for {
                if cur.next == nil {
                    cur.next = node
                    o.lenght++
                    break
                }
                cur = cur.next
            }
        }
    
    }
    
    func (o *List)insert(data,index int){
    
        if index > o.lenght{
            o.append(data)
            return
        }
    
        node := NewListNode(data)
    
        if index <= 1{
            node.next = o.root
            o.root = node
            o.lenght++
            return
        }
    
        var count int
        cur := o.root
        for {
            if count == index -1 {
                tmp := cur.next
                node.next = tmp
                cur.next = node
                o.lenght++
                break
            }
            cur = cur.next
            count++
        }
    }
    
    func (o *List)modify(data,index int) bool {
        if o.root == nil && index > o.lenght{
            return false
        }
        if index <= 1{
            o.root.data = data
            return true
        }
    
        var count int
        cur := o.root
        for {
            if count == index -1 {
                cur.data = data
                break
            }
            if cur.next == nil{
                break
            }
            cur = cur.next
            count++
        }
        return true
    }
    
    func (o *List)del(index int) bool{
        if o.root == nil && index > o.lenght{
            return false
        }
    
        if index == 1{
            o.root = o.root.next
            o.lenght--
            return true
        }
    
        var count int
        cur := o.root
        for {
            if count == index - 1 {
                cur.next = cur.next.next
                o.lenght--
                break
            }
            if cur.next == nil{
                break
            }
            cur = cur.next
            count++
        }
    
        return true
    }
    
    func (o *List)OrDateDel(data int) bool {
        if o.root == nil{
            return false
        }
    
        if o.root.data == data {
            o.root = o.root.next
            o.lenght--
            return true
        }
    
        cur := o.root
        for {
            if cur.data == data{
                cur.data = cur.next.data
                cur.next = cur.next.next
                o.lenght--
                break
            }
            if cur.next == nil{
                break
            }
            cur = cur.next
        }
        return true
    }
    
    
    func (o *List)Get(index int) *ListNode{
    
        if o.root == nil && index > o.lenght {
            return nil
        }
    
        var count int
        cur := o.root
        for {
            if count == index -1 {
                break
            }
            count++
            cur = cur.next
        }
        return cur
    }
    
    func (o *List)GetInDate(data int) *ListNode{
        if o.root == nil  {
            return nil
        }
        if o.root.data == data {
            return o.root
        }
    
        cur := o.root
        for {
            if cur.data == data{
                break
            }
            cur = cur.next
        }
    
        return cur
    }
    
    
    func (o *List)Show(){
        if o.root == nil{
            return
        }
    
        cur := o.root
        for {
            if cur == nil{
                break
            }
            fmt.Println(cur.data)
            cur = cur.next
        }
    }
    
    func (o *List)Lenght()  {
        fmt.Println(o.lenght)
    }
  • 相关阅读:
    python+selenium(环境的安装)
    Eclipse安装和配置
    Java JDK装配置
    Eclipse工具使用技巧总结
    POJ3461 Oulipo
    洛谷P3370 【模板】字符串哈希
    CH1401 兔子与兔子
    洛谷P2347 砝码称重
    洛谷P1038 神经网络
    洛谷P1807 最长路_NOI导刊2010提高(07)
  • 原文地址:https://www.cnblogs.com/zengxm/p/13125730.html
Copyright © 2011-2022 走看看