Big talk Data Structure (v) (Java program)--insertion and deletion of sequential storage structure

Source: Internet
Author: User
Tags array length

Get element operations

For the sequential storage structure of linear tables, we want to implement the getelement operation, which will return the I-position element of the linear table.

Insert operation

Insert Algorithm ideas:

1, if the insertion position is unreasonable, throw an exception

2, if the length of the inserted table is greater than or equal to the array length, throw an exception or dynamic increase capacity

3. Move forward from the last element to the I position, moving each of them backwards one position

4. Fill in position I with insert element

5. Table Length plus 1

Delete operation

1, if the deletion location is unreasonable, throw an exception

2. Remove the insertion element

3. Move from the location of the deleted element to the last element position, moving each of them forward one

4, table length minus one

 Public Interface List {    public Object getelement (intthrows  Exception;       Public void Doinsert (Object obj,intthrows  Exception;      Public void doDelete (intthrows  Exception;}
 Public classSequencelistImplementslist{Static Final intdefualtlenth=10; intMaxSize;//Maximum array length    intSize//Current LengthObject[] list;//Object Array//Sequential Table initialization method     Public voidInitintsize1) {MaxSize= Size1;//The length of this table is Size1List =NewObject[size1]; }    //Non-parametric construction method     Publicsequencelist () {init (defualtlenth); }    //a method for constructing a parameter     PublicSequencelist (intsize2)    {init (size2); } @Override PublicObject GetElement (intIndexthrowsException {if(size==0 | | index<0 | | index>=size) {System.out.println ("Parameter is incorrect"); }        if(Size = =maxSize) {System.out.println (""); }        returnList[index]; } @Override Public voidDoinsert (Object obj,intIndexthrowsException {if(index<0 | | index>size+1) {System.out.println ("Parameter Error"); }        if(Size = =maxSize) {System.out.println ("The linear table is full and cannot be inserted"); }         for(inti=size-1;i>index-1;i--) {List[i+1]=list[i];//the elements after the specified position are moved back one} List[index]=obj; Size++; } @Override Public voidDoDelete (intIndexthrowsException {if(index<0| | index>=size) {System.out.println ("Parameter Error"); }        if(index<size) {             for(inti=size-1;i>index;i--) {List[i-1] =List[i]; }} size--; }    }
 Public classListtest { Public Static voidMain (String args[]) {sequencelist seq=NewSequencelist (20); Try{Seq.doinsert ("AA", 1); Seq.doinsert ("BB", 2); Seq.doinsert ("CC", 3);  for(intj=0;j<seq.size;j++) {System.out.println (Seq.getelement (j));} } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    }
public class Studentlist {public static void main (String args[]) {sequencelist seqlist = new Sequencelist (30); try {seqlist.doinsert (new Student (1, "millet", "female", "seqlist.size"),///each time it is inserted, the last one in the table is inserted Seqlist.doinsert (new Student (2, " Xiao Xu "," Male "," seqlist.size "), for (int i=0;i<seqlist.size;i++) {System.out.println (" + "+ (i+1) +" Student Information: "+ Seqlist.getelement (i). toString ());}}   catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} }}class student{private int id;private string name;private string gender;private int age;public int getId () {return ID;} public void setId (int 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.) {this.age = age;} @Overridepublic String toString () {return "student [id=" + ID + ", name=" + name + ", gender=" + gender+ ", age=" + Age + "]";} Public Student (Int. sid,string name,string Gender,int age) {this.id = Sid;this.name = Name;this.gender = Gender;this.age = A GE;}}

  

Big talk Data Structure (v) (Java program)--insertion and deletion of sequential storage structure

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.