Data Structure Learning notes (2) sequential storage and chain storage of linear tables, and data structure learning chain

Source: Internet
Author: User

Data Structure Learning notes (2) sequential storage and chain storage of linear tables, and data structure learning chain

Linear table: a linear structure of an ordered sequence composed of data elements of the same type.
-- The number of elements in a table is called the length of a linear table.
-- Empty table when no element exists
-- The start position of the table is the header, and the end position is the end of the table.

Sequential storage:

  

1 package test; 2 3/** 4 * linear table (array) 5*6 */7 public class Test {8 private static int m; 9 private static int []; 10 public static void main (String [] args) {11 int [] a = init (); 12 show (a); 13 System. out. println ("element 2 Location:" + query (2, a); 14 show (a); 15 System. out. println (insert (a, 4, 10); 16 show (a); 17 System. out. println (delete (a, 4); 18 show (a); 19} 20/** 21 * initialize 22 */23 public static int [] init () {24 a = new int [10]; 25 for (int I = 0; I <6; I ++) {26 a [I] = I; 27 m = I; 28} 29 return a; 30} 31 32/** 33 * search (return location) 34 */35 public static int query (int I, int []) {36 for (int j = 0; j <. length; j ++) {37 if (a [j] = I) {38 return j; 39} 40} 41 return-1; 42} 43 44/** 45 * insert (insert in I position), first move 46 */47 public static String insert (int [] a, int I, int value) {48 // first determine whether there is a position 49 if (m =. length-1) {50 return "no position"; 51} 52 // judge whether I is legal 53 if (I <0 | I> =. length) {54 return "the location is invalid"; 55} 56 for (int j = m; j> = I; j --) {57 a [j + 1] = a [j]; 58} 59 a [I] = value; 60 m ++; 61 return "element 10 inserted successfully "; 62} 63 64/* 65 * delete 66 */67 public static String delete (int [] a, int I) {68 // determine whether I is valid 69 if (I <0 | I> =. length) {70 return "the location is invalid"; 71} 72 for (int j = I; j <m; j ++) {73 a [j] = a [j + 1]; 74} 75 m --; 76 return "element 10 deleted "; 77} 78 79/** 80 * display element 81 */82 public static void show (int a []) {83 for (int I = 0; I <= m; I ++) {84 System. out. print (a [I] + ""); 85} 86 System. out. println (); 87} 88}

Chain Storage

Package test; import java. util. random;/*** linear table (single-chain table) **/public class Test {public static Node first; public static int m; public static void main (String [] args) {initHead (); show (first); if (queryBySequence (first, 5 )! = Null) {System. out. println ("Search for 5 serial number elements" + queryBySequence (first, 5 ). data);} else {System. out. println ("Incorrect Input Serial Number");} if (queryByValue (first, 58 )! = Null) {System. out. println ("elements with 58 search values:" + queryByValue (first, 58 ). data);} else {System. out. println ("the element whose search value is 58 does not exist");} System. out. println ("insert 20" + insert (100,5, first); show (first); System. out. println (delete (first, 5); show (first) ;}/ *** initialize */public static void initHead () {Random random = new Random (); node node = new Node (random. nextInt (100); first = node; m ++; Node node1 = first; for (int I = 0; I <10; I ++) {node1 = init (node1); m ++ ;}} public static Node init (Node node) {Random random = new Random (); node nodes = new Node (random. nextInt (100); node. next = nodes; return nodes;}/*** search by serial number */public static Node queryBySequence (Node node, int k) {int I = 0; while (node! = Null & I <k-1) {node = node. next; I ++;} if (I = k-1) {return node;} else {return null ;}} /*** search by value */public static Node queryByValue (Node node, int k) {while (Node! = Null & node. data! = K) {node = node. next;} return node;}/*** insert (insert value in position I), first move */public static String insert (int value, int I, Node node) {Node nodes = new Node (20); // Insert the header if (I = 1) {nodes. next = first; first = nodes; return "inserted successfully";} // determines whether the I-1 exists if (queryBySequence (node, I-1) = null) {return "Insertion Location Error";} else {nodes. next = queryBySequence (node, I); queryBySequence (node, I-1 ). next = nodes; return "inserted successfully ";}}/*** Delete */public static String delete (Node node, int I) {if (I = 1) {first = first. next;} if (queryBySequence (node, I) = null) {return "I does not exist";} else {Node node1 = queryBySequence (node, I ). next; queryBySequence (node, I-1 ). next = node1; return "deleted successfully" ;}}/*** display element */public static void show (Node first) {Node node = first; while (node! = Null) {System. out. print (node. data + ""); node = node. next;} System. out. println ();}}

Ps: Node class

  

package test;public class Node {    public int data ;    public  Node next ;    public Node(int data) {        this.data = data;    }    }

 

Time Complexity of the Splitting Algorithm:
T (N) = 2 T (N/2) + cN
= 2 [2 T (N/2 ^ 2)] + cN/2) + cN
= 2 ^ kO (1) + ckN where N/2 ^ k = 1 => k = logn (when the two items are added, take the shard item)
= NlogN

 

Related Article

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.