The Go language implementation of the circular link list

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First, what is a circular link list

The nodes of the loop list form a circle. The tail of the single-linked list is pointed at the beginning to form a single cycle linked list. Linking the tail of a doubly linked list to the beginning forms a two-way circular list. By using a circular list, you can constantly circle around to find the data you need, rather than looking at the beginning of a single linked list every time, you can improve the efficiency of your query.

Today Davido first to achieve a one-way cycle linked list, the implementation of the two-way circular chain is handed to everyone.

The go realization of one-way circular link list

1. Node

The implementation of a one-way cyclic linked list is similar to that of a single-linked list, but for the sake of distinction, we take a different name.

type CNode struct {  data Object  next *cnode}

2, one-way circulation chain list

One-way cycle list The fleet is made up of 5 carriages, and number 1th is the front. In order to represent this relationship, Davido is loaded with the following structure.

Type CList struct {    size UInt64    //Number of cars    head *cnode    //front}

Third, the interface description and implementation

1. Init initialization

Davido is a straightforward person, and the reason for the initialization is to look directly at the previous sections. This time directly on the code.

Func (cList *clist) Init () {    lst: = *clist    lst.size = 0    //no compartment    Lst.head = nil  //No Front}

2. Append Add Data

Adds data to the end of the linked list.

Func (cList *clist) Append (data Object) bool {    node: = new (CNode)    (*node). Data = Data   //Arrange a new compartment, put on the DATA
   if clist.getsize () = = 0 {        (*clist). Head = node  //First car, direct as front    } else {        item: = Clist.gethead ()//Found Car tail for        ; (*item). Next! = Clist.gethead (); Item = (*item). Next {}        (*item). Next = node//Bring the new car to the rear of the car    }    (*node). Next = (*clist). Head/rear end    (*cl IST). size++    return True}

3. Insert data after Insertnext node

After the current node, insert a new node.

Func (cList *clist) insertnext (ELMT *cnode, data Object) bool {    If elmt = nil {        return false    }    node: = NE W (CNode)    //Arrange a new compartment with data    (*node). Data = Data    (*node). Next = (*ELMT). Next/ELMT rear compartment, hanging behind the new compartment    (*ELMT ). Next = node  //ELMT behind the new compartment    (cList). size++    return True}

4. Remove Delete node

Func (cList *clist) Remove (elmt *cnode) Object {    If elmt = nil {        return false    }    Item: = Clist.gethead ()//Find ELMT in front of a carriage for    ; (*item). Next! = ELMT; Item = (*item). Next {}    (*item). Next = (*ELMT). Next//Attach the ropes of the previous carriage directly to the rear compartment    (*clist). size--    return ELMT. GetData ()  //Return to ELMT carriage cargo}

5. GetHead get the start of the list

Func (cList *clist) gethead () *cnode {    return (*clist). Head}

6. GetSize get the number of linked list nodes

Func (cList *clist) getsize () UInt64 {    return (*clist). Size}

7, GetData get the data of the node-mounted
GetData is the method of node to get the goods in the carriage.

Func (node *cnode) GetData () Object {    return (*node). Data}

8. GetNext get the next node
As with GetData, it is a node method for obtaining the next compartment.

Func (node *cnode) GetNext () *cnode {    return (*node). Next}

Code download

Iv. Summary

This is the end of the list, the following chapter Davido will talk about stacks and queues, Davido will be implemented with a linked list.

486 Reads
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.