1. 程式人生 > >php與go實現單鏈表對比

php與go實現單鏈表對比

package 排序 pac list tlist port pre urn h+

實現鏈表操作大概需要定義一個節點結構帶頭結點的鏈表結構,代碼大同小異

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

php與go實現單鏈表對比