Java Data Structures (linear and sequential tables simple implementation)

Source: Internet
Author: User

A linear table is a linear structure that can insert and delete elements at any location, consisting of n elements of the same type. Mainly include sequential table, single linked list, circular single linked list, bidirectional linked list and simulation linked list.  The most widely used are sequential tables and single linked lists. 2The following is the interface of the linear table, the main operations include inserting elements, deleting elements, acquiring elements, getting the number of linear table elements, and judging whether the linear table is empty. 3 Packagecom.linear.table; 4/**5 * Interface method for operation Order Table 6 *@authorMature 7 * 8*/9 Public InterfaceMaturelistinterface {10 Public voidAdd (Object object)throwsException;11 Public voidRemoveintIndexthrowsException;12 PublicObject Get (intIndexthrowsException;13 Public BooleanisEmpty ();14 Public intsize ();15 Public voidInitlist (intsize); 16 17 } 18 19the implementation of the sequential table is as follows:20 Packagecom.linear.table;21st/** * *@authorMature **/26 Public classMaturelistImplementsMaturelistinterface {27intDefaultSize = 10;//Default Capacity28intsize = 0;//number of array elementsobject[] ObjectList =NULL;//storing an array of elementsMaturelist () {//Default Capacity Builder33initlist (defaultsize);34     } 35 36 PublicMaturelist (intListsize) {//specifying the Capacity builder37 This. DefaultSize =listsize;38initlist (defaultsize);39 40     } 41 42@Override43 Public voidInitlist (intsize) { ObjectList =NewObject[size];46 47     } 48 49/**50 * Add an element (starting from 1)*/52@Override53 Public voidAdd (Object object)throwsException {54if(Size = =defaultsize) { 55Throw NewException ("The order table is full and cannot continue inserting"); 56 57}Else { Objectlist[size] = object;//to add an arraysize++; 61         } 62 63     } 64 65/**66 * Delete the specified element (starting from 1)*/68@Override69 Public voidRemoveintIndexthrowsException {object[] Objectlisttmp =NewObject[defaultsize];//Create a TMP array71if(Index < 0 | | Index > DEFAULTSIZE | | index >size) { 72Throw NewException ("parameter error, remove array out of bounds"); 73 74}Else { 75 76 for(inti = index; I <= size-1; i++) { OBJECTLIST[I-1] =Objectlist[i];79 80             } Bayi system.arraycopy (objectList, 0, objectlisttmp, 0, size-1);//to copy the array,ObjectList =NULL; 83 This. objectList = objectlisttmp;//copy the new array to the ObjectListObjectlisttmp =NULL; size--; 86         } 87 88     } 89 90/**91 * Gets the specified element (starting from 1)*/93@Override94 PublicObject Get (intIndexthrowsException {95if(Index > DefaultSize | | | Index < 0) { 96Throw NewException ("parameter error, get array out of bounds"); 97 98}Else { 99returnObjectlist[index-1];100         }101 102     }103 104/**105 * Determine if the linear table (sequential table) is empty 106*/107@Override108 Public BooleanIsEmpty () {109if(Objectlist.length = = 0) {110 111return true;112}Else {113return false;114 115         }116 117     }118 119/**120 * Returns the linear table (sequential table) size 121*/122@Override123 Public intsize () {124 125returnsize;126     }127 128 }129 130Test class:131 Packagecom.linear.table;132/**133 * 134 *@authorMature135 * Test class 136*/137 Public classTest {138 Public Static voidMain (string[] args)throwsException {139 Maturelist maturelist=NewMaturelist (10);140/**141 * Add 5 Elements 142*/143 Maturelist.add ("Mature1");144 Maturelist.add ("Mature2");145 Maturelist.add ("Mature3");146 Maturelist.add ("Mature4");147 Maturelist.add ("Mature5");148 System.out.println ("Number of elements:" +maturelist.size);149 System.out.println ("element 1:" +maturelist.get (1));System.out.println ("Element 2:" +maturelist.get (2));151 System.out.println ("element 3:" +maturelist.get (3));System.out.println ("delete element 3");153 Maturelist.remove (3);//Delete element 1154 System.out.println ("Number of elements:" +maturelist.size);155 System.out.println ("Traversal:");156 for(inti=1;i<=maturelist.size;i++){157 System.out.println ("element:" +Maturelist.get (i));158 159     }160 }161 }162 163Test Results:164 element Number: 5165element 1:mature1166element 2:mature2167element 3:mature3168Delete element 3169 element Number: 4170Traversal:171element: Mature1172element: Mature2173element: Mature4174 elements: Mature5

Java Data Structures (linear and sequential tables simple implementation)

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.