PHP vs. go implementation single-linked list

Source: Internet
Author: User

Implementing a linked list operation would probably require defining a node structure and a linked list structure of the lead nodes, the code is similar

PHP version
/** * Single-linked list node * Class node */class node{/** @var node */public $next = null;    /** @var int */public $data;     /** * Node constructor.    * @param $data */Public function __construct ($data) {$this->data = $data;    }}/** * Single-linked list structure * * Class mylinkedlist */class mylinkedlist{/** @var Node */public $head = null; /** * Add a node * @param $newNode node */Public Function AddNode (node $newNode) {if ($this->hea            D = = null) {$this->head = $newNode;        Return        } $tmpNode = $this->head;        while ($tmpNode->next! = null) {$tmpNode = $tmpNode->next;    } $tmpNode->next = $newNode;    }/** * Delete the index node * * @param int $INDEX * @return BOOL */Public Function deletenode ($index)        {if ($index < 1 | | $index > $this->length ()) {return false;           }//Remove head node if ($index = = 1) { $this->head = $this->head->next;        return true;        } $i = 1;        $preNode = $this->head;        $curNode = $preNode->next;                while ($curNode! = null) {if ($i = = $index) {$preNode->next = $curNode->next;            return true;            } $i + +;            $preNode = $curNode;        $curNode = $curNode->next;    } return true;        }/** * List length * * @return int */Public function length () {$length = 0;        $curNode = $this->head;            while ($curNode! = null) {$length + +;        $curNode = $curNode->next;    } return $length;        }/** * Sort the list */Public function sort () {$curNode = $this->head;            while ($curNode->next! = null) {$nextNode = $curNode->next;    while ($nextNode! = null) {if ($curNode->data > $nextNode->data) {                $temp = $curNode->data;                    $curNode->data = $nextNode->data;                $nextNode->data = $temp;            } $nextNode = $nextNode->next;        } $curNode = $curNode->next;    } return $this->head;        }/** * Print List */Public Function printlist () {$nodeTmp = $this->head; while ($NODETMP! = null) {echo $NODETMP->data.            Php_eol;        $NODETMP = $nodeTmp->next;   }    }}
Test code
$list = new Mylinkedlist (), $list->addnode (new node (5)), $list->addnode (new node (3)), $list->addnode (New node ( 1); $list->addnode (New node (4)), $list->addnode (new node (2)), echo "linked list length:". $list->length (), Php_eol;echo "before sorting". Php_eol, $list->printlist (); $list->sort (); echo "after sort". Php_eol, $list->printlist (); $list->deletenode (3); echo "Delete 3rd node". Php_eol, $list->printlist ();

Post-run output

List length: 5 sort before 53142 sort 12345 Delete 3rd node 1235

  

Go version
Package Mainimport "FMT"//Link Table node type node struct {Next *node data int}//Create a nodes func NewNode (data int) *node {retur n &node{data:data}}//linked list struct type mysinglelist struct {Head *node}//Add a node func (this *mysinglelist) AddNode (NewNode * Node) {if this. Head = = Nil {this. Head = NewNode return} var curnode = this. Head for Curnode.next! = Nil {curnode = curnode.next} curnode.next = newnode}//list length func (This *mysinglel    IST) Length () int {var length int = 0; var Curnode *node = this. Head for Curnode! = Nil {length++ Curnode = curnode.next} return length}//Delete the specified node func (this *mysin glelist) deletenode (index int) bool {if index < 1 | | index > this.    Length () {return false; }//Delete head node if index = = 1 {this. Head = this.    Head.next} var i int = 1; var Prenode = this.    Head; var curnode = Prenode.next for Curnode! = Nil {if i = = Index {prenode.next = CURNODE.NExt return true} i++ Prenode = Curnode Curnode = Curnode.next} return true; }//sorts and returns the head node func (this *mysinglelist) sort () *node {var curnode = this. Head var nextnode *node var temp int for Curnode! = Nil {nextnode = Curnode.next for nextnode! = N Il {if curnode.data > nextnode.data {temp = Curnode.data Curnode.data = Next Node.data nextnode.data = temp} nextnode = nextnode.next} Curnode = C Urnode.next} return this. head}//Print List func (this *mysinglelist) print () {var curnode = this. Head for Curnode! = nil {fmt. Printf ("%v\n", curnode.data) Curnode = Curnode.next}}

Test code

Func Main () {    var list = &mysinglelist{}    list. AddNode (NewNode (5))    list. AddNode (NewNode (3))    list. AddNode (NewNode (4))    list. AddNode (NewNode (1))    list. AddNode (NewNode (2))    FMT. Printf ("Linked list length%v\n", list.) Length ())    FMT. Println ("Pre-order");    List. Print ()    list. Sort ()    FMT. Println ("post-sorting");    List. Sort ()    list. Print ()    FMT. Println ("Delete 3rd element");    List. Deletenode (3)    list. Print ()}

Post-run output

List Length 5 sort before 53412 sort 12345 Delete 3rd element 1235

PHP vs. go implementation single-linked list

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.