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)
    }
  • 相关阅读:
    分治法(求最大子序列和)
    分治法(二分查找)
    自定义选择动画提示
    将图片转为二进制字符串
    根据尺寸压缩图片
    在ios7系统下,scrollView下移20像素
    UIActionSheet警告,提示调用showFromTabBar方法
    使用Xcode5开发时的icon取消高光效果
    duplicate symbol _OBJC_METACLASS_$ 报错记录
    self.view添加UIView时添加动画
  • 原文地址:https://www.cnblogs.com/zengxm/p/13125730.html
Copyright © 2011-2022 走看看