Java data structures and algorithms------linear tables (sequential table structure)

Source: Internet
Author: User

1  PackageiYou.neugle.list;2 3  Public classMyseqlist<t> {4     Private intInitmaxsize = 10;5     Privatet[] list;6     Private intListlen = 0;7 8      Publicmyseqlist () {9          This. Init ();Ten     } One  A      PublicMyseqlist (intsize) { -          This. initmaxsize =size; -          This. Init (); the     } -  -      Public voidInit () { -          This. List = (t[])Newobject[ This. Initmaxsize]; +     } -  +     //Sequential table Add operations A      Public BooleanAdd (T data) { at         if( This. Listlen < This. Initmaxsize) { -              This. List[listlen] =data; -              This. listlen++; -System.out.println ("Insert succeeded"); -             return true; -         } inSYSTEM.OUT.PRINTLN ("Sequential table data is full"); -         return false; to     } +  -     //Sequential Table insert Operation the      Public BooleanInsert (intp, T data) { *         if(P < 0 | | p > This. initMaxSize-1) { $SYSTEM.OUT.PRINTLN ("Invalid Insert data location");Panax Notoginseng             return false; -         } the         if( This. Listlen = = This. Initmaxsize) { +SYSTEM.OUT.PRINTLN ("Sequential table data is full"); A             return false; the         } +         //fault-tolerant processing -         if(P > This. Listlen) { $p = This. Listlen; $              This. list[p] =data; -}Else { -              for(inti = This. listLen-1; I >= p; i--) { the                  This. list[i + 1] = This. List[i]; -             }Wuyi              This. list[p] =data; the         } -          This. listlen++; WuSystem.out.println ("Insert succeeded"); -         return true; About     } $  -     //Sequential Table Delete operations -      Public BooleanDelete (intp) { -         if(P < 0 | | p > This. initMaxSize-1) { ASystem.out.println ("Delete data location illegal"); +             return false; the         } -          for(inti = P; I <= This. listLen-1; i++) { $              This. list[i] = This. list[i + 1]; the         } the          This. listlen--; theSystem.out.println ("Delete succeeded"); the         return true; -     } in  the     //Order table by subscript lookup the      PublicT Get (intp) { About         if(P < 0 | | p > This. initMaxSize-1) { theSYSTEM.OUT.PRINTLN ("Lookup data does not exist"); the             return NULL; the         } +  -System.out.println ("Find successful, data is:" + This. list[p]); the         return  This. list[p];Bayi     } the  the     //Sequential Table Size -      Public intSizeOf () { -         return  This. Listlen; the     } the  the      Public voidPrint () { theSystem.out.print ("["); -          for(inti = 0; I < This. list.length; i++) { theSystem.out.print ( This. List[i]); the             if(I < This. list.length-1) { theSystem.out.print (",");94             } the         } theSystem.out.println ("]"); the     }98}

Java data structures and algorithms------linear tables (sequential table structure)

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.