php data structure analysis summary

1. Linked list data structure

A linked list is a non-consecutive, non-sequential storage structure on a physical storage unit, and the logical order of data elements is achieved through the link order of pointers in the linked list. A linked list consists of a series of nodes (each element in the linked list is called a node), and nodes can be dynamically generated at runtime. Each node consists of two parts: one is the data field that stores the data element, and the other is the pointer field that stores the address of the next node.


2. Linear table data structure

(1) Basic features: the most basic, simplest and most commonly used data structure

In this structure: (emphasis)

1. There is a unique data element called "first";

2. There is a unique data element called "last";

3. Except for the first element, each element has a unique direct predecessor;

4. Except for the last element, each element has a unique immediate successor.

 

(2) Definition:

Linear List: It is a finite sequence consisting of n (n≧0) data elements (nodes) a1, a2, ...an. All nodes in the sequence have the same data type. The number n of data elements is called the length of the linear table.

When n=0, it is called an empty table.

When n>0, the non-empty linear table is written as: (a1, a2,...an)         

a1 is called the first (head) node of the linear table, and an is called the last (tail) node of the linear table.

(3) Sequential storage  of linear table: the nodes of the linear table are stored in a group of storage units with consecutive addresses in logical order. A linear table stored in this way is called a sequential table for short.

Features of sequentially stored linear tables:

1. The logical order of the linear table is consistent with the physical order;

2. The relationship between data elements is represented by the elements being "physically adjacent" in the computer.

PHP implementation of sequential table in linear table

<?php
/**
 * Basic operation of sequence table
 *
 *include
 *1. Initialization of the sequence table __contruct()
 *2. Clear the sequence table __destruct()
 *3. Determine if the sequence table is empty isEmpty()
 *4. Return the length of the sequence table getLength()
 *5. Return an element in the sequence table according to the subscript getElement()
 *6. Returns the position of an element in the sequence table getElementPosition()
 *7. Specify the subscript position to return the element getElemForPos()
 *8. Delete an element in the sequence table according to the element position getDeleteEleForPos()
 */
header("content-type:text/html;charset=utf-8");
class OrderLinearList{
    public $oll;//Sequence table
    /**
     * Sequence table initialization
     *
     * @param mixed $oll
     * @return void
     *  */
    public function __construct($oll=array()){
        $this->oll=$oll;
    }
    /**
     * Clear the sequence table
     *@return void
     */
    public function __destruct(){
        foreach($this->oll as $key=>$value){
            unset($this->oll[$key]);
        }
    }
    /**
     * Determine if the sequence table is empty
     * @return boolean returns true if empty, otherwise returns false
     * */
    public function isEmpty(){
        if(count($this->oll) > 0){
            return false;
        }else{
            return true;
        }
    }
    /**
     * Return the length of the sequence table
     * @return int
     * */
    public function getLength(){
        return count($this->oll);
    }
    /**
     * Returns the element with the subscript $key in the sequence table
     *
     * @param mixed $key the subscript of the sequence table element
     * @return mixed
     * */
    public function getElement($key){
        return $this->oll[$key];
    }
    /**
     * Returns the position of an element in the sequence list
     *
     * @param mixed $value The value of an element in the sequence table
     * @return int starts from 1, if return -1 means the element does not exist
     * strcmp compares two strings to see if they are identical case sensitive
     * */
    public function getElementPosition($value){
        $i=0;
        foreach($this->oll as $val){
            $i++;
            if(strcmp($value,$val) === 0){
                return $i;
            }
        }
        return -1;
    }


    /**
     * Returns an element in the sequence list based on the element position
     *
     * @param int $position element position starts from 1
     * @return array array('value'=>...) element value, array('key'=>...) element subscript */
    public function getElemForPos($position){
        $i=0;
        $len=count($this->oll);
        $position=(int)$position;
        if($position > $len || $position < 1){
            return false;
        }
        foreach($this->oll as $key =>$item){
            $i++;
            if($i == $position){
                return array('value'=>$item,'key'=>$key);
            }
        }
    }

    /**
     * Delete an element in the sequence table according to the element position
     *
     * @param int $position element position starts from 1
     * @return bool returns true on success, false on failure
     * */
    public function getDeleteEleForPos($position){
        $len=count($this->oll);
        $i=0;
        $position=(int)$position;
        if($position > $len || $position < 1){
            return false;
        }
        foreach($this->oll as $k=>$v){
            $i++;
            if($i == $position){
            }else{
                if(is_int($k)){
                    $ oll [] = $ v;
                }else{
                    $oll[$k]=$v;
                }
            }
        }
        $this->oll=$oll;
        if(count($this->oll) == $len){
            return false;
        }
        return true;
    }
}

$oll=new OrderLinearList(array('xu'=>'Xu Dianyang',32,"Yes",'dianyang'=>10,455));
/ / Determine whether the sequence table is empty, return false to indicate that it is not empty
var_dump($oll->isEmpty());
echo "<br />";
//return the length of the sequence table and return 6
echo $oll->getLength();
echo "<br />";
//return an element in the sequence table according to the subscript
var_dump($oll->getElement(1));
echo "<br/>";
//returns the position of an element in the sequence table
echo $oll->getElementPosition("Yes");
echo "<br/>";

//Return an element in the sequence table according to the element position
echo "------find=";
var_dump($oll->getElemForPos(2));
echo "<br />";

//Delete an element in the sequence table according to the element position
var_dump($oll->getDeleteEleForPos(1));

echo "<br />";



$people = array("Peter", "Joe", "Glenn", "Cleveland");

echo current($people) . "<br>";
echo next($people) . "<br>";
echo prev($people);
?>









$arr = array(3,5,8,4,9,6,1,7,2);  
echo implode(" ",$arr)."<br/>";
//Using the principle of linear table to achieve deletion
function delete_array_element($arr,$pos){  
    $length = count($arr);  
    if($pos<1 || $pos>$length){  
        return "Error deleting position!";  
    }  
    for($i=$pos-1;$i<$length-1;$i++){  
        $arr[$i] = $arr[$i+1];  
    }  
    array_pop($arr);  
    return $arr;  
}  
$pos = 3;  
echo "<br/>After removing the element at position {$pos}: ";  
echo implode(' ',delete_array_element($arr,$pos))."<br/>";  

3. Sequential stack data structure

That is, the sequential storage structure of the stack is to use a group of storage units with consecutive addresses to store the data elements from the bottom of the stack to the top of the stack in turn, and at the same time, a pointer top is attached to indicate the position of the top element of the stack in the sequential stack. (also similar to our array)

    

class Stack{  
    //The stack is directly initialized with the default value, and the stack can also be initialized by the constructor  
    private $top = -1;  
    private $maxSize = 5;  
    private $stack = array();  
    // push the stack  
    public function push($elem){  
        if($this->top >= $this->maxSize-1){  
            echo "Stack is full!<br/>";  
            return;  
        }  
        $this->top++;  
        $this->stack[$this->top] = $elem;  
    }  
    // pop the stack  
    public function pop(){  
        if($this->top == -1){  
            echo "Stack is empty!";  
            return ;  
        }  
        $elem = $this->stack[$this->top];  
        unset($this->stack[$this->top]);  
        $this->top--;  
        return $elem;  
    }  
    //print stack  
    public function show(){  
        for($i=$this->top;$i>=0;$i--){  
            echo $this->stack[$i]." ";  
        }  
        echo "<br/>";  
    }  
}  
$stack = new Stack();  
$stack->push(3);  
$stack->push(5);  
$stack->push(8);  
$stack->push(7);  
$stack->push(9);  
$stack->push(2);  
$stack->show();  
$stack->pop();  
$stack->pop();  
$stack->pop();  
$stack->show();  

4. Two-way queue data structure


A queue is a linear list that allows insertions at one end (the tail of the queue) and deletions at the other end (the head of the queue). The rear pointer points to the tail of the queue, and the front pointer points to the head of the queue.
A queue is a linear list of "first in, last out" (FIFO) or "last in, last out" (LILO).
  Queue operations include
  (1) Enqueue operation: insert an element from the end of the queue;
  (2) Exit operation: delete an element from the head of the queue.
Circular queue: s=0 means the queue is empty, s=1 and front=rear means the queue is full


class  
    private $queue = array();  
        public function addFirst($item){//The head joins the queue  
        array_unshift($this->queue,$item);  
    }  
    public function addLast($item){//Tail queue  
        array_push($this->queue,$item);  
    }  
    public function removeFirst(){//Head out of the queue  
        array_shift($this->queue);  
    }  
    public function removeLast(){//Tail dequeue  
        array_pop($this->queue);  
    }  
    public function show(){//print  
        foreach($this->queue as $item){  
            echo $item." ";  
        }  
        echo "<br/>";  
    }  
}  
$deque = new Deque();  
$deque->addFirst(2);  
$deque->addLast(3);  
$deque->addLast(4);  
$deque->addFirst(5);  
$deque->show();  



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325567923&siteId=291194637