Java implementation sequence structure linear list function code _java

Source: Internet
Author: User
Tags int size

Nonsense not much to say that directly on the code

Copy Code code as follows:

Package com.ncu.list;

/**
*
* Sequential Structure Linear list
*
*
*/
public class Squencelist<t> {
private int size; The length of a linear table
Private object[] Listarray;
private int currensize = 0; Data in the current linear table

Public Squencelist () {

}

public squencelist (int size) {
this.size = size;
Listarray = new Object[size];
}

public void arraycopy (int index) {
Object newarray[] = new Object[size];
for (int i = 0; i < currensize; i++) {
if (I >= index) {
Newarray[i] = listarray[i + 1];
} else {
Newarray[i] = Listarray[i];
}
}
Listarray = NewArray;
NewArray = null; Releasing resources
}

/**
* remove element from index position
*
* @param index
*/
public void Remove (int index) {
index = index-1;
if (Index < 0 | | | index > currensize) {
SYSTEM.OUT.PRINTLN ("linear table index out of Bounds");
}
if (currensize = = 0) {
SYSTEM.OUT.PRINTLN ("Linear table is empty");
} else {
currensize--;
Arraycopy (index);
if (currensize = = 0) {
Listarray = null;
}
}
}

   /**
     * Removing elements from element content
     *
      * @param element
     */
    public void Removelocate (T element) {
        for (int i = 0; i < currensize;) {
            if (element.equals (Listarray[i])) {
                Remove (i + 1);
           } else {
                 i++;
           }
       }
   }

/**
* Inserting data from the end of a linear table
*
* @param element
*/
public void Add (T element) {
if (currensize > Size | | currensize < 0) {
SYSTEM.OUT.PRINTLN ("linear table index out of Bounds");
} else {
Listarray[currensize] = element;
currensize++;
}
}

private void Insert (T element, int index) {
index = index-1;
if (currensize > Size | | currensize < 0 | | Index < 0
|| Index >= currensize) {
SYSTEM.OUT.PRINTLN ("linear table index out of Bounds");
} else {
Object newarray[] = new Object[size];
for (int i = 0; i < currensize; i++) {
if (I >= index) {
Newarray[index] = element;
Newarray[i + 1] = Listarray[i];
} else {
Newarray[i] = Listarray[i];
}

           }
            listarray = NewArray;
            newarray = null;
            currensize++;
       }
   }

   /**
     Insert data at the specified index
     *
      * @param element
     * @param index
     */
     public void Add (T element, int index) {
        if (index = = size) {
            Add (Element);
       } else {
             Insert (element, index);
       }
   }

   /**
     * Delete the last element of a linear table
     */
     public void Delete () {
        if (IsEmpty ()) {
             System.out.println ("Linear table is empty, cannot delete");
       } else {
             listarray[currensize-1] = null;
            currensize--;
       }
   }

   /**
     * Interpreting linear table IS null
     *
      * @return
     */
    public boolean IsEmpty () {
         if (currensize = 0) {
             return true;
       } else {
             return false;
       }
   }

/**
* Find the appropriate element based on the index
*
* @param index
* @return
*/
Public T get (int index) {
T obj = null;
if (IsEmpty () | | | index > CURRENSIZE | | Index < 0) {
SYSTEM.OUT.PRINTLN ("Linear table is null, cannot delete");
} else {
obj = (T) listarray[index-1];
}

return obj;
}

/**
* Clear the linear table
*/
public void Clear () {
size = 0;
currensize = 0;
}

/**
* Get the number of current elements in the linear table
*
* @return
*/
public int size () {
return currensize;
}

public void Showlist () {
if (Currensize > 0) {
for (int i = 0; i < currensize; i++) {
System.out.println (Listarray[i]);

}
} else {
SYSTEM.OUT.PRINTLN ("Linear table is empty");
}

System.out.println ("------------");
}

public static void Main (string[] args) {
squencelist<integer> list = new squencelist<integer> (10);
}
}

Related Article

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.