实现链表操作大概需要定义一个节点结构和带头结点的链表结构,代码大同小异
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 排序前 5 3 1 4 2 排序后 1 2 3 4 5 删除第3个节点 1 2 3 5
Go版本
package main import "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 排序前 5 3 4 1 2 排序后 1 2 3 4 5 删除第3个元素 1 2 3 5