Data Structure Java Implementation 02 ---- linear table and sequence table, java02 ----
[Statement]
Reprinted, but keep the original source of the article → _ →
Life One: http://www.cnblogs.com/smyhvae/
Source: http://www.cnblogs.com/smyhvae/p/4758808.html
Contact: smyhvae@163.com
[Body]
Content of this section:
- Linear Structure
- ABSTRACT Data Types of linear tables
- Sequence Table
- Ordered table Application
I,Linear Structure:
If a data element sequence meets the following requirements:
(1) except the first and last data elements, each data element has only one precursor Data Element and one successor data element;
(2) The first data element does not have a precursor data element;
(3) The last data element does not have any successor data element.
Such a data structure is called a linear structure.
II,Linear tableABSTRACT Data Type:
1. Concepts of abstract data types in linear tables:
ABSTRACT Data Types of a linear table mainly include two aspects: the operator set that combines a dataset with the dataset.
Data Set:
It can be expressed as a0, a1, a2,... an-1. The data type of each data element can be any type.
The operation set includes the following:
1. Calculate the number of elements
2. Insert
3. Delete
4. Search
5. check whether it is empty.
2. Design a Java interface for abstract data types of linear tables:
The Code is as follows:
// Linear Table interface public interface List {// obtain the length of a linear table public int size (); // determine whether the linear table is empty public boolean isEmpty (); // insert the public void insert (int index, Object obj) throws Exception; // delete the public void delete (int index) throws Exception; // obtain the public Object get (int index) throws Exception element at the specified position ;}
Then let the subclass implement this interface.
Iii. Sequence Table: (the physical storage structure is continuous and the size is fixed)
1. Concept of sequence table:
The computer has two basic storage structures (physical storage structure): sequential structure and discrete structure. Implemented using the sequential structureLinear tableCalledSequence Table. As shown in:
// Linear Table interface public interface List {// obtain the length of a linear table public int size (); // determine whether the linear table is empty public boolean isEmpty (); // insert the public void insert (int index, Object obj) throws Exception; // delete the public void delete (int index) throws Exception; // obtain the public Object get (int index) throws Exception element at the specified position ;}
(2) SequenceList. java :( core code)
1 public class SequenceList implements List {2 3 // the maximum length of the default sequence table is 4 final int defaultSize = 10; 5 // The maximum length is 6 int maxSize; 7 // The current length is 8 int size; 9 // Object array 10 Object [] listArray; 11 12 13 public SequenceList () {14 init (defaultSize ); 15} 16 17 public SequenceList (int size) {18 init (size); 19} 20 21 // Initialization Method of the sequence table 22 private void init (int size) {23 maxSize = size; 24 this. size = 0; 25 listArray = new Object [size]; 26} 27 28 @ Override29 public void delete (int index) throws Exception {30 // TODO Auto-generated method stub31 if (isEmpty () {32 throw new Exception ("the sequence table is empty and cannot be deleted! "); 33} 34 if (index <0 | index> size-1) {35 throw new Exception (" parameter error! "); 36}37 // move element 38 for (int j = index; j <size-1; j ++) {39 listArray [j] = listArray [j + 1]; 40} 41 size --;42} 43 44 @ Override45 public Object get (int index) throws Exception {46 // TODO Auto-generated method stub47 if (index <0 | index> = size) {48 throw new Exception ("parameter error! "); 49} 50 return listArray [index]; 51} 52 53 @ Override54 public void insert (int index, Object obj) throws Exception {55 // TODO Auto-generated method stub56 // if the current linear table is full, 57 if (size = maxSize) {58 throw new Exception ("the sequence table is full, unable to insert! "); 59} 60 // whether the inserted position number is valid 61 if (index <0 | index> size) {62 throw new Exception (" parameter error! "); 63} 64 // move the element65 for (int j = size-1; j> = index; j --) {66 listArray [j + 1] = listArray [j]; 67}6869 listArray [index] = obj; // whether or not the size of the current linear table is zero, this statement can be executed normally, that is, 70 size ++ can be inserted normally;71 72} 73 74 @ Override75 public boolean isEmpty () {76 // TODO Auto-generated method stub77 return size = 0; 78} 79 80 @ Override81 public int size () {82 // TODO Auto-generated method stub83 return size; 84} 85}
Let's take a look at the insert () method of row 54th: If you want to insert a data in the index position, then the elements after the index will move one bit further. Note the following:
Insert operation: when moving an element, you must move it from the back to the back. You cannot move it from the back to the back. Otherwise, the element will be overwritten..
Delete element: when moving an element, you must move it from the beginning to the end.
(3) test class:
Public class Test {public static void main (String [] args) {SequenceList list = new SequenceList (20); try {list. insert (1, 0,100); list. insert (0, 50); list. insert (1, 20); for (int I = 0; I <list. size; I ++) {System. out. println ("Number of" + I + "is" + list. get (I) ;}} catch (Exception e) {e. printStackTrace ();}}}
We should pay attention to what the insert rule is, otherwise we will feel that the order of output in this sequence table is very strange.
Running effect:
Fixed size (At the beginning, the maximum length of the sequence table must be fixed);Inserting and deleting elements requires moving a large amount of data.
5. Application of sequence tables:
Design an order table to save the information of 100 students, save the information of the following three students, and print the output.
// Student public class Students {private String id; // student id private String name; // name private String gender; // gender private int age; // age public Students () {} public Students (String sid, String name, String gender, int age) {this. id = sid; this. name = name; this. gender = gender; this. age = age;} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getGender () {return gender;} public void setGender (String gender) {this. gender = gender;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String toString () {return "student ID:" + this. getId () + "name:" + this. getName () + "Gender:" + this. getGender () + "Age:" + this. getAge ();}}
(4) Test. java:
1 public class Test {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 // TODO Auto-generated method stub 8 SequenceList list = new SequenceList (100); 9 10 try {11 list. insert (list. size, new Students ("S0001", "Zhang San", "male", 18); // The first parameter list. size indicates that I insert data at the last position of the sequence table (the length of the current linear table. In this row, the size is equal to the 012 list. insert (list. size, new Students ("S0002", "Li Si", "male", 19); 13 list. insert (list. size, new Students ("S0003", "Wang Wu", "female", 21); 14 15 for (int I = 0; I <list. size; I ++) {16 System. out. println (list. get (I); 17} 18 19} catch (Exception ex) {20 ex. printStackTrace (); 21} 22} 23 24}
Note the comment of Line 1: The first parameter list. size indicates that each time I insert an object at the last position of the sequence table (The position of the length of the current linear table). In this case, the time series are output in the order of Zhang San, Li Si, and Wang Wu.
Running effect: