线性表和单向链表

今天介绍2个简单的数据结构:线性表&单向链表

 

线性表:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/6
 * Time: 15:38
 */

/**
 * 线性表 更利于数据的查找 时间O(n)
 * 不利于删除或新增 时间O(nlogn)
 * 在查询操作使用的比较频繁时,使用顺序表会好一些;在插入、删除操作使用的比较频繁时,使用单链表会好一些。
 * Class arrayList
 */
class arrayList
{
    private $arr;
    private $length;
    const MAX_SIZE=100;

    /**
     * arrayList constructor.
     * @param $arr
     * @param $length
     */
    function __construct($arr,$length)
    {
        $this->arr=$arr;
        $this->length=$length;
    }


    /**
     * 根据位置查找值
     * @param $n
     * @return string
     */
    function findValue($n)
    {
        if($n<1||$n>$this->length)
            return '起始位置超出索引范围';
        return $this->arr[$n-1];
    }

    /**
     * 根据值来查找位置
     * @param $value
     */
    function  findSite($value)
    {
        if(empty($value)||!isset($value))
            return '值不允许为空!';
        foreach ($this->arr as $key=>$v)
        {
            if($v==$value) return $key+1;
        }
        return '您查找的值不存在';
    }

    /**
     * 在指定位置插入值
     * @param $val
     * @param $n
     */
    function insertVal($val,$n)
    {
        if($n<1||$n>($this->length+1))
            return '索引查过数组的索引范围';
        for($h=$this->length;$h>=$n;$h--)
        {
            $this->arr[$h] = $this->arr[$h-1];
        }
        if($n>$this->length)
            $this->arr[$this->length]=$val;
        else
            $this->arr[$n-1] = $val;
        $this->length++;
        return $this->arr;
    }

    /**
     * 删除指定元素
     * @param $n
     */
    function deleteVal($n)
    {
        if($n<1||$n>$this->length)
            return '索引查过数组的索引范围';
        for($k=$this->length;$k>=$n;$k--)
        {
            $this->arr[$k-2]=$this->arr[$k-1];
        }
        unset($this->arr[$this->length-1]);
        $this->length--;
        return $this->arr;
    }

    function showValue()
    {
        if($this->length<=0)
            echo '数组为空!';
        foreach ($this->arr as $key=>$val)
        {
            echo 'key:'.$key.' value:'.$val.'</br>';
        }
    }

    function __destruct()
    {
            echo '销毁链表!';
    }

}
$arr = array(1,3,4,6,7);
$arrayList = new arrayList($arr,5);
//查找
echo $arrayList->findValue(1);
echo $arrayList->findSite(4);
//新增
$arrayList->insertVal('xxx',3);
$arrayList->showValue();
//删除
$arrayList->deleteVal(3);
$arrayList->showValue();

  

链表:

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。它的数据是以结点(类型一般为结构体)来表示的,每个结点的构成:数据(类型为要存储的数据的类型) + 指针(结构体指针),数据就是链表里具体要存储的东西,指针就是用来把每个节点都连接起来,使它们形成一个链状。

结点:

链表:

<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2016/11/6

 * Time: 15:38

 */



class node

{

    public $id;

    public $name;

    public $next;



    function __construct($id,$name)

    {

        $this->id=$id;

        $this->name=$name;

    }

}



/**

 * 单向链表 便于删除和插入 时间O(n)

 * 不利于查找 时间O(n)

 * Class linkedList

 */

class linkedList

{

    private $head;



    /**

     * linkedList constructor

     * 初始化头节点.

     * @param null $id

     * @param null $name

     */

    function __construct($id=null,$name=null)

    {

        $this->head=new node($id,$name);

    }



    function getLinkedListLength()

    {

        $i=0;

        $current = $this->head;

        while($current->next!=null){

            $i++;

            $current = $current->next;

        }

        return $i;

    }



    function insertNode($node)

    {

        $current = $this->head;

        while($current->next!=null)

        {

            if($current->next->id>$node->id) break;

            $current = $current->next;

        }

        $node->next =$current->next;

        $current->next = $node;



    }



    function delNode($id)

    {

        $current = $this->head;

        $flag = false; //是否存在该节点

        while($current->next!=null)

        {

            if($current->next->id == $id){

                $flag = true;

                break;

            }

            $current = $current->next;

        }

        if($flag)

            $current->next = $current->next->next;

        else

            echo '该节点不存在!';

    }



    function  getLinecked()

    {

        $current = $this->head;

        if($current->next==null)

            echo '链表为空!';

        while($current->next!=null)

        {

            echo 'id:'.$current->next->id.' name:'.$current->next->name.'</br>';

            if($current->next->next == null) break;

            $current=$current->next;

        }



    }



    function getLinkedNameById($id)

    {

        $current = $this->head;

        if($current->next==null)

            echo '链表为空!';

        while($current->next!=null)

        {

            if($current->id==$id) {

                break;

            }

            $current = $current->next;

        }

        return $current->name;

    }



    function updateLinkedNode($node)

    {

        $current = $this->head;

        if($current->next==null)

            echo '链表为空!';

        while($current->next!=null)

        {

            if($current->id == $node->id) break;

            $current=$current->next;

        }

        $current->name=$node->name;



    }



    function __destruct()

    {

        echo '销毁单向链表!'.'</br>';

    }



}



$linkes = new linkedList();

$linkes->insertNode(new Node(1,'xll'));

$linkes->insertNode(new Node(2,'cyq'));

$linkes->insertNode(new Node(4,'cyq1'));

$linkes->insertNode(new Node(7,'cyq12'));

$linkes->getLinecked();

//删除

$linkes->delNode(1);

$linkes->getLinecked();

//根据id

$linkes->getLinkedNameById(2);

//更新node

$linkes->updateLinkedNode(new node(2,'xulele'));

$linkes->getLinecked();

  

总论:

一般如果读的操作比较频繁,则推荐使用线性表;如果插入或者删除的操作比较频繁则推荐使用单向链表

原文地址:https://www.cnblogs.com/xulele/p/6037621.html