Java Implementation order Chain list

Source: Internet
Author: User

Sequencelist.java file

Package List;public class Sequencelist {private int list_init_size = 5;//The original size of the linked list private int INCREMENT =1;//list increment size private Object []sqlist = null;//list private int curindex=0;//current position/** * Initialize linked list * */public void Initlist () {sqlist = new object[list_i Nit_size];} /** * insert element into linked list * */public void insertlist (Object o) {if (curindex>list_init_size-1)//Determine if the current list is full {// Allocate space System.out.println ("Newly Allocated Space") from the new list; List_init_size+=increment;object []temp = new object[list_init_size];for (int i=0;i<curindex;i++) {temp[i]=SqList[ I];} Sqlist=null; Sqlist=temp;} If you do not let it contain duplicate elements in the list, add this code/*if (Iscontain (o)) {System.out.println ("This element is already included in the list" +o); Else{}*/sqlist[curindex++]= o;} /** * Determine if the list contains an element * */boolean Iscontain (Object o) {for (int i=0;i<curindex;i++) {if (Sqlist[i].equals (o)) {return true;}} return false;} /** * Delete an element in the list * * If the containing duplicate element is deleted * */public void Delete (Object o) {for (int i=0;i<curindex;i++) {if (Sqlist[i].equals (O)) {f or (int j=i;j<curindex-1;j++) {sqlist[j]=sqlist[j+1];} Curindex--;continue;} if (i==curindex-1) {SysteM.OUT.PRINTLN ("This element does not exist" +o);}}} /** * Gets an element in the list * */public Object getelement (int i) {if (i <= 0 | | i > Curindex) {System.out.println ("Get position exceeds number of elements in the list "+curindex);} return sqlist[i-1];} /** * Print List * */public void print () {for (int i=0;i<curindex;i++) {System.out.print (sqlist[i]+ "\ t");} System.out.println ();}}

Main function test

Package List;public class Sequencelistmain {public static void main (string[] args) {sequencelist sqlist = new Sequencelist (); Sqlist.initlist (); sqlist.insertlist (1); sqlist.insertlist (2); sqlist.insertlist (3); sqlist.insertlist (4); Sqlist.insertlist (5); sqlist.insertlist (6); Sqlist.delete (5); Sqlist.delete (9); sqlist.insertlist (1); SqList.print ( ); Sqlist.delete (1); Sqlist.print (); System.out.println ("The 2nd element is:" +sqlist.getelement (1)); System.out.println ("The 4th element is:" +sqlist.getelement (4));}}

  

Java Implementation order Chain list

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.