go 单链表 linkedlist

package myfunc

import (
	"fmt"
	"log"
	"os"
	"reflect"
	"runtime"
)

//ElemType is list type
type ElemType interface{}

//Node is list node
type Node struct {
	Data ElemType
	Next *Node
}

//LinkedList is single list
type LinkedList struct {
	Head *Node
}

//NewNode create a new node
func NewNode(x ElemType) *Node {
	return &Node{x, nil}
}

//NewLinkedList create a new list
//the head node data is the length of the linked list
func NewLinkedList() *LinkedList {
	head := &Node{0, nil}
	return &LinkedList{head}
}

//Append insert an element after
//the list
func (list *LinkedList) Append(node *Node) {
	if !list.isNewNode(node) {
		list.printLog("EROOR: It's not a new node!")
		return
	}
	tmpList := list.Head
	for {
		if tmpList.Next == nil {
			break
		} else {
			tmpList = tmpList.Next
		}
	}
	tmpList.Next = node
	list.sizeInc()
}

//Prepend insert an element at the front of
//the list
func (list *LinkedList) Prepend(node *Node) {
	if !list.isNewNode(node) {
		list.printLog("Prepend EROOR: It's not a new node!")
		return
	}
	tmpList := list.Head
	node.Next = tmpList.Next
	tmpList.Next = node

	list.sizeDec()
}

//DeleteElem delete a designation element from
//the list
func (list *LinkedList) DeleteElem(data ElemType) bool {
	var lastList *Node
	lastList = list.Head
	tmpList := list.Head.Next
	for {
		if tmpList.Data != data {
			if tmpList.Next != nil {
				lastList = tmpList
				tmpList = tmpList.Next
			} else {
				break
			}
		} else {
			lastList.Next = tmpList.Next
			list.sizeDec()
			return true
		}
	}
	return false

}

//Find searce the element from the list
func (list *LinkedList) Find(data ElemType) (*Node, bool) {
	tmpList := list.Head
	for tmpList.Data != data {
		if tmpList.Next != nil {
			tmpList = tmpList.Next
		} else {
			fmt.Println("Error :this data is not in the list!!!")
			return nil, false
		}
	}
	return tmpList, true

}

//IsEmpty judge the list is empty
func (list *LinkedList) IsEmpty() bool {
	return list.Head.Next == nil
}

//Length return the length of list
func (list *LinkedList) Length() int {
	return int(reflect.ValueOf(list.Head.Data).Int())
}

//sizeInc increase the list length
func (list *LinkedList) sizeInc() {
	v := int(reflect.ValueOf((*list.Head).Data).Int())
	list.Head.Data = v + 1
}

//sizeDec decrease the list length
func (list *LinkedList) sizeDec() bool {
	v := int(reflect.ValueOf((*list.Head).Data).Int())
	if v < 1 {
		list.printLog("Error: sizeDec!!!")
		return false
	}
	list.Head.Data = v - 1
	return true
}

//isNewNode determine the node is acceptable
func (list *LinkedList) isNewNode(node *Node) bool {
	tmplist := list.Head
	for tmplist.Next != nil {
		if tmplist.Next == node {
			return false
		}
		tmplist = tmplist.Next
	}
	return true
}

//printLog print the err log
func (list *LinkedList) printLog(err string) {
	log.Println(err)
	_, file, line, _ := runtime.Caller(1)
	log.Println(file, line)
	_, file, line, _ = runtime.Caller(2)
	log.Println(file, line)
	os.Exit(1)
}

//PrintList print the details of the linkedlist
func (list *LinkedList) PrintList() {
	if list.IsEmpty() {
		list.printLog("Error:list is empty!!!")
		return
	}
	tmpList := list.Head.Next
	fmt.Println("The Elements is:")
	i := 0
	for ; ; i++ {
		if tmpList.Next == nil {
			break
		} else {
			fmt.Printf("Node: %d , Value: %d\n", i+1, tmpList.Data)
			tmpList = tmpList.Next
		}
	}
	fmt.Printf("Node: %d , Value: %d\n", i+1, tmpList.Data)

}

 test 

package main

import (
	"log"
	ds "myfunc"
)

func main() {
	var list *ds.LinkedList
	list = ds.NewLinkedList()

	for i := 0; i < 10; i++ {
		node := ds.NewNode(i)
		list.Append(node)
	}

	if _, b := list.Find(1); b {
		log.Println("FIND!")
	}

	list.DeleteElem(1)

	log.Println("len: ", list.Length())

	node := ds.NewNode(2)
	list.Prepend(node)

	log.Println(list.IsEmpty())

	list.PrintList()
}

猜你喜欢

转载自blog.csdn.net/hqm178933/article/details/83001160