Go數據結構之單鏈表(LinkedList)


單鏈表-LinkedList

    鏈表Linked list)是一種常見的基礎數據結構,但是並不會按線性的順序存儲數據,而是在每一個節點里存到下一個節點的指針。由於不必須按順序存儲,鏈表在插入的時候可以達到O(1),比順序表快得多,但是查找一個節點或者訪問特定編號的節點則需要O(n)的時間。

    以下是我用Go語言實現的單鏈表:

/**
 * Created with IntelliJ IDEA.
 * Date: 4/24/14
 * Time: 5:11 PM
 * 這是一個自定義的單鏈表數據結構.
 * Author: requelqi@gmail.com
 * Copyright 2014 Requelqi. All rights reserved.
 */
package LinkedList

import (
    "reflect"
    "fmt"
    "errors"
)

//鏈表中的數據類型
type ElementType interface {}

//節點結構體
type INode struct {
    X ElementType //節點
    next *INode   //下一節點指針
}

//鏈表結構體
type LinkedList struct {
    Head *INode //有一個頭節點
}

func NewINode(x ElementType, next *INode) *INode {
    return &INode{x, next}
}
func NewLinkedList() *LinkedList {
    //頭節點的數據域存儲鏈表數據元素的大小(不包括頭節點)
    head := &INode{0, nil}
    return &LinkedList{head}
}

/**
 * 判斷鏈表是否為空
 */
func (list *LinkedList) IsEmpty() bool {
    return list.Head.next == nil
}

/**
 * 鏈表的長度
 */
func (list *LinkedList) Length() int {
    return int(reflect.ValueOf(list.Head.X).Int())
}

/**
    向鏈表后端Append元素
 */
func (list *LinkedList) Append(node *INode) {
    current := list.Head
    for {
        if current.next == nil {
            break
        }
        current = current.next
    }
    current.next = node
    list.sizeInc()
}

/**
    每次都在前端插入元素
 */
func (list *LinkedList) Prepend(node *INode) {
    current := list.Head
    node.next = current.next
    current.next = node
    list.sizeInc()
}

/**
    根據指定的數據查找*INode
 */
func (list *LinkedList) Find(x ElementType) (*INode, bool) {
    empty := list.IsEmpty()
    if empty {
        fmt.Println("This is an empty list")
        return nil, false
    }
    current := list.Head
    for current.next != nil {
        if current.X == x {
            return current, true
        }
        current = current.next
    }
    if current.X == x {
        return current, true
    }
    return nil, false
}
/**
    刪除給定元素的節點
 */
func (list *LinkedList) Remove(x ElementType) error {
    empty := list.IsEmpty()
    if empty {
        return errors.New("This is an empty list")
    }
    current:=list.Head
    for current.next!=nil{
       if current.next.X==x{
          current.next=current.next.next
           list.sizeDec()
           return nil
       }
        current=current.next
    }
    return nil
}

/**
    內部函數,處理頭節點記錄的鏈表大小
 */
func (list *LinkedList) sizeInc() {
    v := int(reflect.ValueOf((*list.Head).X).Int())
    list.Head.X = v + 1
}
/**
    內部函數,處理頭節點記錄的鏈表大小
 */
func (list *LinkedList) sizeDec() {
    v := int(reflect.ValueOf((*list.Head).X).Int())
    list.Head.X = v - 1
}

/**
    打印鏈表信息
 */
func (list *LinkedList) PrintList() {
    empty := list.IsEmpty()
    if empty {
        fmt.Println("This is an empty list")
        return
    }
    current := list.Head.next
    fmt.Println("The elements is:")
    i := 0
    for ; ; i++ {
        if current.next == nil {
            break
        }
        fmt.Printf("INode%d ,value:%v -> ", i, current.X)
        current = current.next
    }
    fmt.Printf("Node%d value:%v", i + 1, current.X)
    return

}

  done~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM