골 랑 으로 이 루어 진 단 방향 링크

데이터 구 조 를 복습 하고 골 랑 으로 단 방향 링크 를 실현 합 니 다.
package main

import "fmt"

type Object interface{}

type Node struct {
    Data Object
    next *Node
}

type List struct {
    size uint64
    head *Node
    tail *Node
}

func (list *List) Init() {
    (*list).size = 0
    (*list).head = nil
    (*list).tail = nil
}

//        
func (list *List) Append(node *Node) bool {
    if node == nil {
        return false
    }

    (*node).next = nil //        ,  next
    if (*list).size == 0 {
        (*list).head = node
    } else {
        oldTail := (*list).tail //     
        (*oldTail).next = node  //     next      
    }

    (*list).tail = node //        
    (*list).size++
    return true
}

//   i        
func (list *List) Insert(i uint64, node *Node) bool {
    if node == nil || i > (*list).size || (*list).size == 0 {
        return false
    }

    if i == 0 {
        (*node).next = (*list).head
        (*list).head = node
    } else {
        preNode := (*list).head
        for j := uint64(1); j < i; j++ {
            preNode = (*preNode).next
        }

        (*node).next = (*preNode).next //              next
        (*preNode).next = node         //     next     
    }
    (*list).size++

    return true
}

//          
func (list *List) Remove(i uint64) bool {
    if i >= (*list).size {
        return false
    }

    if i == 0 {
        preHead := (*list).head     //        
        (*list).head = preHead.next //      next     

        //         ,       
        if (*list).size == 1 {
            (*list).head = nil
            (*list).tail = nil
        }
    } else {
        preNode := (*list).head
        for j := uint64(1); j < i; j++ {
            preNode = (*preNode).next
        }

        node := (*preNode).next     //           
        (*preNode).next = node.next //          next       next,      

        //       ,        
        if i == ((*list).size - 1) {
            (*list).tail = preNode
        }
    }

    (*list).size--

    return true
}

//       
func (list *List) RemoveAll() bool {
    (*list).Init()
    return true
}

//          
func (list *List) Get(i uint64) *Node {
    if i >= (*list).size {
        return nil
    }

    node := (*list).head
    for j := uint64(0); j < i; j++ {
        node = (*node).next
    }

    return node
}

//            
func (list *List) IndexOf(data Object) int64 {
    pos := int64(-1)
    node := (*list).head
    if node.Data == data {
        return 0
    }

    for j := uint64(1); j < (*list).size; j++ {
        if node != nil {
            node = (*node).next
            if node != nil && node.Data == data {
                pos = int64(j)
                break
            }
        }
    }
    return pos
}

//       
func (list *List) GetSize() uint64 {
    return (*list).size
}

//      
func (list *List) GetHead() *Node {
    return (*list).head
}

//      
func (list *List) GetTail() *Node {
    return (*list).tail
}

func main() {
    var l List
    l.Init()

    node1 := &Node{Data: 11111}
    l.Append(node1)

    node2 := &Node{Data: 22222}
    l.Append(node2)

    node3 := &Node{Data: 33333}
    l.Append(node3)

    node4 := &Node{Data: "insert"}
    l.Insert(1, node4)

    node5 := &Node{Data: "head"}
    l.Insert(0, node5)

    node6 := &Node{Data: "tail"}
    l.Append(node6)

    l.Remove(0)
    l.Remove(1)
    l.Remove(3)

    pos1 := l.IndexOf(22222)
    pos2 := l.IndexOf(44444)
    fmt.Println(pos1, pos2)

    fmt.Println(l.GetHead(), l.GetTail(), l.GetSize())
    fmt.Println()

    //l.RemoveAll()

    for i := uint64(0); i < l.size; i++ {
        fmt.Println(l.Get(i))
    }
}

좋은 웹페이지 즐겨찾기