Java sequence table implementation

Source: Internet
Author: User
Import Java. util. student; Class data {// simulate a class's student record string key; string name; int age;} class sltype {static final int maxlen = 100; data [] listdata = new data [maxlen + 1]; int listlen; // number of existing nodes in the sequence table void slinit (sltype SL) {SL. listlen = 0;} int sllength (sltype SL) {return (SL. listlen);} // insert node int slinsert (sltype SL, int N, data) {int I; If (SL. listlen> = maxlen) {system. out. println ("sequence table full, cannot insert node"); Return 0 ;}if (n <1 | N> SL. listLen-1) {system. out. println ("Insertion Sequence Number error, cannot insert node"); Return 0;} // move the data in the sequence table backward for (I = SL. listlen; I> = N; I --) {SL. listdata [I + 1] = SL. listdata [I];} SL. listdata [N] = data; SL. listlen ++; return 1 ;}// append node int sladd (sltype SL, data) {If (SL. listlen> = maxlen) {system. out. println ("sequence table full, cannot insert node"); Return 0;} SL. listdata [++ SL. listlen] = data; return 1 ;}// Delete the node int sldelete (sltype SL, int N) {int I; If (n <1 | n> SL. listlen + 1) {system. out. println ("incorrect number input, cannot insert node"); Return 0 ;}// forward (I = N; I <SL. listlen; I ++) {SL. listdata [I] = SL. listdata [I + 1];} SL. listlen --; return 1 ;}// query node data slfindbynum (sltype SL, int N) {If (n <1 | n> SL. listlen + 1) {system. out. println ("No. input error, cannot insert node"); return NULL;} return SL. listdata [N];} // search for the node int slfindbycont (sltype SL, string key) {int I; for (I = 1; I <= SL. listlen; I ++) {If (SL. listdata [I]. key. compareto (Key) = 0) {return I ;}} return 0 ;}// display all nodes int slall (sltype SL) {int I; for (I = 1; I <= SL. listlen; I ++) {system. out. println (SL. listdata [I]. key + "#" + SL. listdata [I]. name + "#" + SL. listdata [I]. age);} return 0;} public class sequentiallist {public static void main (string [] ARGs) {int I; sltype SL = new sltype (); // define the sequence table variable // data Data = new data (); // define the node to save the data type variable data pdata; // define the node to save the pointer variable string key; // Save the keyword system. out. P RINT ("sequence table operation demo! \ N "); SL. slinit (SL); // initialize the sequence table system. Out. Print (" the initialization sequence table is complete! \ N "); Specify input = new partition (system. in); do {// cyclically add node data system. out. print ("input node (student ID name age):"); data Data = new data (); data. key = input. next (); data. name = input. next (); data. age = input. nextint (); If (data. age! = 0) // if the age is not 0 {If (SL. sladd (SL, data) = 0) // If the node fails to be added {break; // exit the endless loop} else // if the age is 0 {break; // exit the endless loop} while (true); system. out. print ("\ n sequence table node order: \ n"); SL. slall (SL); // display all node data system. out. print ("\ n number of the node to be retrieved:"); I = input. nextint (); // The number of input nodes pdata = SL. slfindbynum (SL, I); // search for the node if (pdata! = NULL) // If the returned node pointer is not null {system. out. printf ("Node % d: (% s, % s, % d) \ n", I, pdata. key, pdata. name, pdata. age);} system. out. print ("\ n keyword of the node to be searched:"); Key = input. next (); // enter the keyword I = SL. slfindbycont (SL, key); // search by keyword. The return node number pdata = SL. slfindbynum (SL, I); // query by serial number, return the node pointer if (pdata! = NULL) // If the node pointer is not null {system. out. printf ("Node % d: (% s, % s, % d) \ n", I, pdata. key, pdata. name, pdata. age );}}}

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.