Data structures, data structures and algorithms
Linear table: A finite sequence of 0 or more data elements (note: The following are integer data simulations)
A sequential storage structure (a data element that stores linear tables once in a contiguous storage unit)
1.1 Three properties: The starting position of the storage space; maximum storage capacity; current length
Note: The array length is the length of the storage space that holds the linear table (which is generally constant), but the language can dynamically increase the capacity, resulting in performance loss;
The linear table length is the number of data elements;
The linear table is the number starting from 1, corresponding to the position of the array 0
1.2 Get elements, insert elements, delete elements (show in code)
1.3 Advantages and disadvantages of sequential structure:
Pros: There is no need to add additional storage space to represent the logical relationship between elements in a table, and you can quickly access any element in a table
Disadvantage: Insert and delete operations need to move a large number of elements, when the linear table length is large, it is difficult to determine the capacity of storage space, causing storage space ' fragments '
//simulating linear tables with one-dimensional arrays classSequential_structure {//Length of linear table Private $num= 0; //Array Length Private $len= 0; //Array Emulation Private $arr=Array(); /** * Initialize structure * @param Int $len Maximum array length * @param array $arr arrays * @return */ Public function__construct ($len,Array $arr) { $this->len =$len; $length=Count($arr); if($length> 0 &&$length<=$len) { $this->arr =$arr; $this->num =$length; } } /** * Get linear table elements * @param Int $i need to get the first few elements * @return*/ Public functionGet_elem ($i) { if($this->num = = 0 | |$i< 1 | |$i>$this->num)//determine if a lookup is reasonable return false; return $this->arr[$i-1];//return data, Time complexity O (1) } /** * Insert element (in sequential structure, after inserting element, all subsequent data will be moved back, average time complexity O (1)): * If the insertion position is unreasonable, failure * If the linear length is greater than the array length, the error is returned or the capacity is increased dynamically * Move forward from the last element to the I position, moving them backwards one position * insert elements into I position * @param Int $i need to be inserted into the first few elements * @param in T $elem inserted node * @return bool*/ Public functionInsert_elem ($i,$elem) { if($this->num = =$this->len)//sequential linear table is full return false; if($i< 1 | |$i> ($this->NUM+1))//I not within range return false; if($i<=$this->num)//if the data insertion position is not at the end { for($k=$this->num-1;$k>=$i-1; --$k)//after all the elements move backward one $this->arr[$k+1] =$this->arr[$k]; } $this->arr[$i-1] =$elem;//inserting elements++$this-num; return true; } /** * Delete elements (in sequential structure, after inserting elements, all subsequent data must be moved forward, average time complexity O (1)): * If the deletion position is unreasonable, failure * Delete element * from the last element to delete After traversing to the end, move them forward one position * @param Int $i The number of elements required for warehousing * @return BOOL*/ Public functionDelete_elem ($i) { if($this->num = = 0)//linear table is empty return false; if($i< 1 | |$i>$this->num)//Delete location is incorrect return false; if($i<$this->num)//Delete location is not footer { for($k=$i;$k<$this->num; ++$k)//move forward $this->arr[$k-1] =$this->arr[$k]; } unset($this->arr[$this->num-1]); --$this-num; return true; } /** * Get order form * @return*/ Public functionGet_arr () {return $this-arr; } /** * Get length * @return*/ Public functionGet_len () {return Array(' num ' =$this->num, ' len ' =$this-Len); } } $link=NewSequential_structure (10,[1,4,8,7]); Echo $link->get_elem (2); Var_dump($link->insert_elem (5,5)); Var_dump($link-Get_arr ()); Var_dump($link-Get_len ()); Var_dump($link->delete_elem (1)); Var_dump($link-Get_arr ()); Var_dump($link->get_len ());
Output:
Booleantruearray (size=5) 0 = int 1 1 = int 4 2 = int 8 3 =&G T int 7 4 = int 5array (size=2) ' num ' + int 5 ' len ' and int 'BOOLEANtruearray (size=4) 0 = int 4 1 = int 8 2 = int 7 3 => ; int 5Array (size=2) ' num ' = = int 4 ' len ' + int 10
Two-linked list storage structure (n-node chain form a linked list)
2.1 Single-linked list (with array emulation)
The first node in the 2.1.1 list is stored as a head pointer (usually to facilitate the operation of the linked list, a head node is attached before the first node of a single-linked list)
Tip pointer: A pointer to the first node of the list, if the list has a head node, which is a pointer to the head node; whether the linked list is empty or not, the head pointer is not empty
Head node: Before the node of the first element
/** * Using a one-dimensional array to simulate linear table * Array (' data ' =>data, ' cur ' =>cur) data for the storage, cur for the next array element subscript*/ classSimple_link {//Array Length Private $len= 0; //Array Emulation Private $arr=Array(); //idle subscript in array Private $space _arr=Array(); /** * Initialize structure * @param Int $len Maximum array length * @param array $arr arrays * @return */ Public function__construct ($len,Array $arr) { $this->len =$len; $length=Count($arr); $this->arr[0][' data ' =$length; $this->arr[0][' cur '] = 0; for($i= 0;$i<$length; ++$i) $this->arr[$i[' cur '] =$i+1;//point of analog linked list if($length) $this->arr[$length[' cur '] = 0;//The last node pointer is empty. for($i=$length+ 1;$i<=$len-$length; ++$i)//Free Array Array_unshift($this->space_arr,$i); } /** * Get linear table elements: * Initialize $j starting from 1 * When $j< $i, traverse the list * @param Int $i need to get the first few elements * @r Eturn*/ Public functionGet_elem ($i) { if($i< 1 | |$i>$this->arr[0][' data ']) return false; $j= 1; $cur=$this->arr[0][' cur '];//points to the first node while($j<$i) { $cur=$this->arr[$cur[' cur ']; ++$j; }return $this->arr[$cur[' Data ']; } /** Insert element: * Initialize $J starting from 1 * When $j< $i, traverse list * insert element into I position * @param Int $i need to be inserted into Several elements * @param Int $elem the inserted node * @return bool*/ Public functionInsert_elem ($i,$elem) { $len=$this->arr[0][' data ' + 1; if($i< 1 | |$i>$len) return false; $j=$this->malloc ();//get the Idle subscript if(!$j) return false; $this->arr[$j[' data '] =$elem; $k= 1; $index= 0; $cur= !Empty($this->arr[0][' cur ')?$this->arr[0][' cur ': 0;//points to the first node while($k<$i) { //record current cur and next cur $index=$cur; $cur=$this->arr[$index[' cur ']; ++$k; } //Change pointer pointing $this->arr[$index[' cur '] =$j; $this->arr[$j[' cur '] =$cur; ++$this->arr[0][' data ']; return true; } /** * Delete element: * Initialize $J starting from 1 * When $j< $i, traverse list * Remove I position * @param Int $i need to delete the first few elements * @return BOOL*/ Public functionDelete_elem ($i) { $len=$this->arr[0][' data ']; if($i< 1 | |$i>$len) return false; $k= 1; $index= 0; $cur= !Empty($this->arr[0][' cur ')?$this->arr[0][' cur ': 0;//points to the first node while($k<$i) { //record current cur and next cur $index=$cur; $cur=$this->arr[$index[' cur ']; ++$k; } //Change pointer pointing $this->arr[$index[' cur '] =$this->arr[$cur[' cur ']; $this->free ($cur); unset($this->arr[$cur]); --$this->arr[0][' data ']; return true; } /** * Get an idle node subscript, which is equivalent to applying for an empty node * @return*/ Private functionmalloc () {if(Empty($this-Space_arr)) return false; return Array_pop($this-Space_arr); } /** * Release node * @param Int $cur the node subscript to be recycled*/ Private functionFree$cur) { Array_push($this->space_arr,$cur); } /** * print * @return*/ Public functionPrint_arr () {$i= 0; if(!Empty($this->arr[0][' data '])) { while($this->arr[$i[' cur ']) { $i=$this->arr[$i[' cur ']; Echo $this->arr[$i[' Data ']. ' '; } } } /** * Get length * @return*/ Public functionGet_len () {return Array(' num ' =$this->arr[0][' data ', ' len ' =$this-Len); } } $link=NewSimple_link (10,Array()); Var_dump($link->insert_elem (1,5)); Var_dump($link->insert_elem (2,4)); Var_dump($link->insert_elem (1,6)); Var_dump($link->delete_elem (3)); Echo $link-Print_arr (); Var_dump($link-Get_len ()); Output:Boolean true Boolean true Boolean true Boolean true6 5Array(size=2) ' num ' = int 2 ' len ' = + int 10
http://www.bkjia.com/PHPjc/1119062.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119062.html techarticle Data structures , data structures and algorithms linear tables: 0 or more finite sequences of data elements (note: The following are all used for integer data simulation) a sequential storage structure (with an address connected ...