Java Simulation for orderly table operations

Source: Internet
Author: User

ImportJava.util.Scanner;classdata{//Create a data type that is used as each unit of an ordered tableString Key;    String name; intAge ;}classsltype{//Create an ordered table class that contains an array of data types, and a fixed-length    Static Final intmaxlen=100; Data[] ListData=NewData[maxlen+1]; intListlen; voidSlinit (Sltype SL) {//An ordered table is initialized by placing his length at 0.Sl. Listlen=0; }        intSllength (Sltype SL) {//the way to get the ordered table length is to return the ordered table length ..... I don't think it's necessary to encapsulate this method.        returnSL.    Listlen; }        intSlinsert (Sltype SL,intN,data DATA) {//inserting the method, passing in the ordered table to be inserted, and data type, then add 1 to the length.        if(SL. Listlen>=maxlen) {//always look for an orderly table before insertingSystem.out.print ("The order table is full, cannot insert node n"); return0; }        if(n<1| | N>maxlen) {//Check that the input node is reasonableSystem.out.println ("Insert incorrect n value"); return0; }         for(intI=sl. listlen;i>=n;i--) {//from the last one, move the data backwards, moving to the position you want to insert.Sl. listdata[i+1]=SL.        Listdata[i]; } SL. Listdata[n]=data;//The data type data is passed into the array of SL.Sl. listlen++;//the length attribute of an ordered table plus 1        return1; }            intSladd (Sltype sl,data DATA) {//add one to the last surface of the ordered table        if(SL. listlen>=maxlen) {System.out.println ("The order table is full."); return0; } SL. listdata[++sl. Listlen]=data;//First + +, all subscripts from 1 onwards        return1; }        intSldelete (Sltype SL,intN) {//Delete an ordered TABLE element        if(n<1| | N>SL. Listlen) {System.out.println ("N does not meet the requirements"); return0; }         for(intI=n;i<sl. listlen;i++) {//starting with Nth, the last value in turn overwrites the previousSl. Listdata[n]=sl. Listdata[n+1]; } SL. Listlen--;//length minus one        return1; } DATA Slfindbynum (Sltype SL,intN) {//find an ordered table by index        if(n<1| | N>sl. listlen+1) {//Check if n conforms to specificationSystem.out.println ("N Non-conforming to Regulations")); return NULL; }        returnSL.    Listdata[n]; }        intSlfindbycont (Sltype sl,string key) {//index According to the value of key         for(intI=1;i<=sl. listlen;i++){            if(SL. Listdata[i].key.compareto (Key) ==0){                returnI//returns the subscript if a match is returned, returning the first qualified            }        }        return0; }            intSlall (Sltype SL) {//Print all the ordered table elements//int i;     for(intI=1;i<=sl. listlen;i++) {System.out.printf ("(%s,%s,%d) \ n", SL. Listdata[i].key,sl. Listdata[i].name,sl. Listdata[i].age);//Format Print    }    return0; }        } Public classList { Public Static voidMain (string[] args) {//TODO Code application logic hereSltype SL =NewSltype (); inti; Sl.        Slinit (SL); Scanner in=NewScanner (system.in);  Do{//design a dead loop that jumps out of the loop only if it meets a certain conditionDATA Data=NewDATA ();//define a data that is used to hold each set of inputsSystem.out.println ("Enter key name Age:"); Data.key=In.next (); Data.name=In.next (); Data.age=In.nextint (); if(data.age!=0) {SL. Sladd (sl,data);//if Age is not 0 (a rule) continue to insert data into an ordered table}Else{                 Break;//If you find that the input age is 0, stop            }                    } while(true); Sl. Slall (SL);//Show all ordered table dataSYSTEM.OUT.PRINTLN ("The order to be removed is:"); I=In.nextint (); DATA Pada=sl. Slfindbynum (SL, i);//get data based on the index        if(pada!=NULL) {System.out.printf ("(%s,%s,%d) \ n", Pada.key,pada.name,pada.age); } System.out.println ("The keyword to be removed is:"); String Key=In.next (); I=sl. Slfindbycont (SL, key);//gets the index obtained from the keyword valueDATA HADA=SL. Slfindbynum (SL, i);//get data based on the index                if(hada!=NULL) {System.out.printf ("(%s,%s,%d) \ n", Hada.key,hada.name,hada.age); }    }    }

Java simulation implements ordered table operations

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.