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)
    }
  • 相关阅读:
    php (一)
    php 运算符
    Python 元组
    Python 深拷贝和浅拷贝的区别
    Python 列表
    Python 字符串
    Python 循环控制
    Python 随机数,数学
    bzoj5018 [Snoi2017]英雄联盟
    bzoj5015 [Snoi2017]礼物
  • 原文地址:https://www.cnblogs.com/zengxm/p/13125730.html
Copyright © 2011-2022 走看看