Go language link list exercises

Source: Internet
Author: User

Package Mainimport "FMT"//define nodes type node struct {Data intnext *node}/** return first node * H header node */func getfirst (H *node) *node {if H .  Next = nil {return Nil}return h.next}/** returns the last node * H header node */func getlast (H *node) *node {if H.next = nil {return Nil}i: = Hfor I.next! = Nil {i = i.nextif I.next = nil {return I}}return nil}//take length func getlength (h *node) int {var I int = 0n: = Hfor N.next! = Nil {i++n = N.next}return i}//Insert a node//h: Head node//d: node to be inserted//p: position to insert func insert (H, D *node, p int) bool {if H.N Ext = = Nil {H.next = Dreturn true}i: = 0n: = hfor N.next! = Nil {i++if i = = p {if N.next.next = nil {n.next = Dreturn tru e} else {d.next = N.nextn.next = D.nextreturn True}}n = n.nextif N.next = nil {n.next = Dreturn True}}return false}//take out the specified Node func getloc (H *node, p int) *node {if p < 0 | | p > GetLength (h) {return nil}var i int = 0n: = hfor N.next! = Nil { I++n = n.nextif i = = p {return N}}return Nil}func main () {var h nodeli: = []int{2, 4, 3}for _, L: = range Li {var d noded. Data = Linsert (&h,&d, L)}var i nodels: = []int{5, 6, 4}for _, S: = range ls {var dd nodedd.data = Sinsert (&i, &AMP;DD, s)}sum: = 0su m = Getloc (&h, 3). data*100 + Getloc (&h, 2). Data*10 + getloc (&h, 1). Data + Getloc (&i, 3). data*100 + Getloc (&i, 2). Data*10 + getloc (&i, 1). Datafmt.println (SUM)}

  

Go language link list exercises

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.