Data structure, data structure and algorithm-PHP Tutorial

Source: Internet
Author: User
Data structure, data structure, and algorithm. Data structure, data structure, and algorithm linear table: a finite sequence of zero or more data elements (note: The following are all integer data simulations) A sequential storage structure (using a segment of address to connect to a data structure, data structure and algorithm

Linear table: finite sequence of zero or multiple data elements (note: The following are all integer data simulations)

Sequential storage structure (store the data elements of a linear table at a time with a sequential storage unit)
1.1 three attributes: the starting position of the bucket; maximum storage capacity; current length
Note: The array length is the length of the storage space for storing linear tables (usually unchanged). However, the language can dynamically increase the capacity, resulting in performance loss;
The length of a linear table is the number of data elements;
A linear table starts from 1 and corresponds to the position of array 0.
1.2 obtain, insert, and delete elements (displayed in code)

1.3 Advantages and disadvantages of ordered structure:
Advantage: you do not need to add additional storage space to represent the logical relationship between elements in the table. you can quickly access any element in the table.
Disadvantages: the insert and delete operations require a large number of elements to be moved. when the length of a linear table is large, it is difficult to determine the storage space capacity'

// Use a one-dimensional array to simulate the linear table class Sequential_Structure {// The length of the linear table private $ num = 0; // The array length private $ len = 0; // array simulation private $ arr = array (); /*** initialization structure ** @ param Int $ len maximum Array length * @ param Array $ arr Array * @ 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 the linear table element * @ param Int $ I the elements to be obtained * @ return */public function get_elem ($ I) {if ($ this-> num = 0 | $ I <1 | $ I> $ this-> num) // You can check whether the search result is reasonable and return false; return $ this-> arr [$ i-1]; // return data, time complexity O (1)}/*** insert an element (in a sequential structure, after inserting an element, all subsequent data must be moved back, with the average time complexity O (1): * If the insertion position is unreasonable, Failure * if the linear length is greater than the array length, returns an error or dynamically increases the size * from the last element to the position I, move them backward to a position * insert the elements into the I position * @ param Int $ I need to insert the elements to the first element * @ param Int $ elem insert node * @ return bool */ public function insert_elem ($ I, $ elem) {if ($ this-> num = $ this-> len) // return false when the ordered linear table is full; if ($ I <1 | $ I> ($ this-> num + 1) // if I is not within the range, return false; if ($ I <= $ this-> num) // if the data insertion location is not at the end of the table {for ($ k = $ this-> num-1; $ k >=$ I-1; -- $ k) // move all the elements behind it one by one. $ this-> arr [$ k + 1] = $ this-> arr [$ k];} $ this-> arr [$ i-1] = $ elem; // Insert element + + $ this-> num; return true;}/*** delete an element (in a sequential structure, after an element is inserted, all subsequent data must be moved forward. average time complexity O (1): * If the deletion location is unreasonable, failed * delete element * traverse from last deleted element to last, move them forward to a position * @ param Int $ I the elements to be stored * @ return bool */public function delete_elem ($ I) {if ($ this-> num = 0) // The linear table is empty, return false; if ($ I <1 | $ I> $ this-> num) // The deletion location is incorrect. return false; if ($ I <$ this-> num) // The deletion location is not the end of the table {for ($ k = $ I; $ k <$ this-> num; ++ $ k) // forward $ this-> arr [$ k-1] = $ this-> arr [$ k];} unset ($ this-> arr [$ this-> num-1]); -- $ this-> num; return true ;} /*** obtain the sequence table * @ return */public function get_arr () {return $ this-> arr ;} /*** get length * @ return */public function get_len () {return array ('num' => $ this-> num, 'len' => $ this-> len) ;}$ link = new Sequential_Structure (10, [,]); 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:
Boolean truearray (size = 5) 0 => int 1 1 => int 4 2 => int 8 3 => int 7 4 => int 5 array (size = 2) 'num' => int 5 'len' => int 10 boolean truearray (size = 4) 0 => int 4 1 => int 8 2 => int 7 3 => int 5 array (size = 2) 'num' => int 4 'len' => int 10

Binary linked list storage structure (n node chains form a linked list)
2.1 Single-chain table (simulated by array)
2.1.1 The storage position of the first node in the linked list is the head pointer (a head node is usually attached to the first node of the single-linked list to facilitate operations on the linked list)
Note head pointer: pointer to the first node of the linked list. if the linked list has a head node, this is a pointer to the head node. no matter whether the linked list is empty or not, the header pointer is not empty.
Header node: Before the node of the first element

/*** Use a one-dimensional array to simulate a linear table * array ('data' => data, 'cur' => cur) data to store data, cur is the subscript of the next array element */class Simple_Link {// array length private $ len = 0; // array simulation private $ arr = array (); // idle subscript in the array private $ space_arr = array (); /*** initialization structure ** @ param Int $ len maximum Array length * @ param Array $ arr Array * @ 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; // simulate a linked list pointing to if ($ length) $ this-> arr [$ length] ['cur'] = 0; // the last node pointer is null for ($ I = $ length + 1; $ I <= $ len-$ length; ++ $ I) // idle array array_unshift ($ this-> space_arr, $ I);}/*** get linear table elements: * initialize $ j from 1 * when $ j <$ I, traversal chain table * @ param Int $ I the elements to be obtained * @ return */public function get_elem ($ I) {if ($ I <1 | $ I> $ this-> arr [0] ['data']) return fals E; $ j = 1; $ cur = $ this-> arr [0] ['cur']; // point to the first node while ($ j <$ I) {$ cur = $ this-> arr [$ cur] ['cur']; ++ $ j ;} return $ this-> arr [$ cur] ['data'];}/*** insert element: * initialize $ j from 1 * when $ j <$ I, in the traversal chain table * insert an element to the I position * @ param Int $ I need to insert the element to the several elements * @ param Int $ elem inserted node * @ return bool */public function insert_elem ($ i, $ elem) {$ len = $ this-> arr [0] ['data'] + 1; if ($ I <1 | $ I> $ len) return false; $ j = $ this-> malloc (); // Get 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; // point to the first node while ($ k <$ I) {// record the current and next cur $ index =$ cur; $ cur = $ this-> arr [$ index] ['cur']; ++ $ k ;} // change the pointer to $ this-> arr [$ index] ['cur'] = $ j; $ this-> arr [$ j] ['cur'] = $ cur; ++ $ this-> arr [0] ['data']; return true ;} /*** delete element: * initialize $ j from 1 * when $ j <$ I, the traversal link table * deletes the I position * @ param Int $ I the elements to be deleted * @ return bool */public function delete_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; // point to the first node while ($ k <$ I) {// record the current and next cur $ index =$ cur; $ cur = $ this-> arr [$ index] ['cur']; ++ $ k ;} // change the pointer to $ this-> arr [$ index] ['cur'] = $ this-> arr [$ cur] ['cur']; $ this-> free ($ cur); unset ($ this-> arr [$ cur]); -- $ this-> arr [0] ['data']; return true;}/*** get idle node subscript, which is equivalent to applying for an empty node * @ return */private function malloc () {if (empty ($ this-> space_arr) return false; return array_p Op ($ this-> space_arr);}/*** release node * @ param Int $ node subscript to be recycled by cur */private function free ($ cur) {array_push ($ this-> space_arr, $ cur);}/*** print * @ return */public function print_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 function get_len () {return array ('num' => $ this-> arr [0] ['data'], 'len' => $ this-> len );}} $ link = new Simple_Link (10, array (); var_dump ($ link-> insert_elem )); 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 6 5 array (size = 2) 'num' => int 2 'len' => int 10

Linearlinear table: a finite sequence of zero or multiple data elements (note: The following is a simulation of integer data) a sequential storage structure (connected with an address segment...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.