实现链表操作大概需要定义一个节点结构和带头结点的链表结构,代码大同小异
PHP版本
/** * 单链表节点 * Class Node */class Node{ ???/** @var Node */ ???public $next = null; ???/** @var int */ ???public $data; ???/** ????* Node constructor. ????* @param $data ????*/ ???public function __construct($data) ???{ ???????$this->data = $data; ???}}/** * 单链表结构 * * Class MyLinkedList */class MyLinkedList{ ???/** @var Node */ ???public $head = null; ???/** ????* 添加一个节点 ????* @param $newNode Node ????*/ ???public function addNode(Node $newNode) ???{ ???????if ($this->head == null) { ???????????$this->head = $newNode; ???????????return; ???????} ???????$tmpNode = $this->head; ???????while ($tmpNode->next != null) { ???????????$tmpNode = $tmpNode->next; ???????} ???????$tmpNode->next = $newNode; ???} ???/** ????* 删除第index个节点 ????* ????* @param int $index ????* @return bool ????*/ ???public function deleteNode($index) ???{ ???????if ($index < 1 || $index > $this->length()) { ???????????return false; ???????} ???????//删除头节点 ???????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; ???} ???/** ????* 链表长度 ????* ????* @return int ????*/ ???public function length() ???{ ???????$length = 0; ???????$curNode = $this->head; ???????while ($curNode != null) { ???????????$length++; ???????????$curNode = $curNode->next; ???????} ???????return $length; ???} ???/** ????* 对链表进行排序 ????*/ ???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; ???} ???/** ????* 打印链表 ????*/ ???public function printList() ???{ ???????$nodeTmp = $this->head; ???????while ($nodeTmp != null) { ???????????echo $nodeTmp->data . PHP_EOL; ???????????$nodeTmp = $nodeTmp->next; ???????} ???}}
测试代码
$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 "链表长度:" . $list->length(), PHP_EOL;echo "排序前" . PHP_EOL;$list->printList();$list->sort();echo "排序后" . PHP_EOL;$list->printList();$list->deleteNode(3);echo "删除第3个节点" . PHP_EOL;$list->printList();
运行后输出
链表长度:5排序前53142排序后12345删除第3个节点1235
Go版本
package mainimport "fmt"//链表结点type Node struct { ???Next *Node ???Data int}//创建一个节点func NewNode(data int) *Node { ???return &Node{Data:data}}//链表结构体type MySingleList struct { ???Head *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}//链表长度func (this *MySingleList) Length() int { ???var length int = 0; ???var curNode *Node = this.Head ???for curNode != nil { ???????length++ ???????curNode = curNode.Next ???} ???return length}//删除指定节点func (this *MySingleList) DeleteNode(index int) bool { ???if index < 1 || index > this.Length() { ???????return false; ???} ???//删除头结点 ???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;}//排序并返回头结点func (this *MySingleList) Sort() *Node { ???var curNode = this.Head ???var nextNode *Node ???var temp int ???for curNode != nil { ???????nextNode = curNode.Next ???????for nextNode != nil { ???????????if curNode.Data > nextNode.Data { ???????????????temp = curNode.Data ???????????????curNode.Data = nextNode.Data ???????????????nextNode.Data = temp ???????????} ???????????nextNode = nextNode.Next ???????} ???????curNode = curNode.Next ???} ???return this.Head}//打印链表func (this *MySingleList) Print() { ???var curNode = this.Head ???for curNode != nil { ???????fmt.Printf("%v\n", curNode.Data) ???????curNode = curNode.Next ???}}
测试代码
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("链表长度%v\n", list.Length()) ???fmt.Println("排序前"); ???list.Print() ???list.Sort() ???fmt.Println("排序后"); ???list.Sort() ???list.Print() ???fmt.Println("删除第3个元素"); ???list.DeleteNode(3) ???list.Print()}
运行后输出
链表长度5排序前53412排序后12345删除第3个元素1235
php与go实现单链表对比
原文地址:https://www.cnblogs.com/chenqionghe/p/8337181.html