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);
}
}