Unidirectional linked list Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport "FMT" type Object interface {}//nodes type node struct {data Object next *node}//one-way linked list Type list struct {He Ad *nodetail *nodesize UInt64}//Initialize func (list *list) init () {(*list). Size = 0//The list is empty at this time (*list). Head = nil//No head (*list  ). Tail = nil//No tail}//adds data func (list *list) Append (node *node) bool {if node = = Nil {return false} (*node). Next = nil// Put the new element in the single-linked list if (*list). Size = = 0 {(*list). Head = node} else {oldtail: = (*list). Tail (*oldtail). Next = node}//Adjust the trailing position, and the number of elements in the list Volume (*list). Tail = node//node becomes the new tail (*list). size++//number of elements increased return true}//Insert data func (list *list) insert (i uint64,node *n ODE) bool {//NULL node, index out of range, and empty list cannot be inserted if node = nil | | i > (*list). Size | | (*list). Size = = 0 {return False}if i = = 0 {//Direct row first, also lead brother-can (*node). Next = (*list). Head (*list). Head = node} else {//found The previous element Preitem: = (*list). Headfor J: = 1; UInt64 (j) < I; J + + {//number front I element Preitem = (*preitem). next}//The original element is placed behind the new element, and the new element is placed behind the previous element (*node). Next = (*preitem). Next (*preitem). Next = Preitem} (*list). Size++return true}//Delete element func (list *list) remove (I UInt64, node *node) bool {if I >= (*list). Size {return false}if I  = = 0 {//delete head node = (*list). Head (*list). Head = (*node). Nextif (*list). Size = = 1 {//If there is only one element, the tail is also adjusted (*list). Tail = nil}} else {preitem: = (*list). Headfor J: = 1; UInt64 (j) < I; J + + {Preitem = (*preitem). Next}node = (*preitem). Next (*preit EM). Next = (*node). Nextif i = = ((*list). size-1) {//If the end of the deletion, the tail pointer needs to be adjusted (*list). Tail = Preitem}} (*list). Size--return true}// Gets the element func (list *list) get (i UInt64) *node {if I >= (*list). Size {return Nil}item: = (*list). Headfor J: = 0; UInt64 (j) & Lt I J + + {//From head number I item = (*item). Next}return Item}func Main () {var list= list{}list. Init () for i:=1;i<100; i++ {var node=node{data:i}list. Append (&node)}var node=list. Get (+) fmt. Printf ("Current node location:%+q \ n, data:%d \ n", Node,node.data) var deletenode=&node{} var result=list. Remove (35,deletenode) fmt. Printf ("Delete succeeded%+q \ n", result) var node2=list. Get (+) fmt. Printf ("Current node location:%+q \ n, data:%d \ n ", Node2,node2.data)} 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.