public class ShunXuBiaoShi<E> {
private int size=-1;
private E[] data=(E[])new Object[10];;
//判断是否为空
public boolean listEmpty() {
return size == 0;
}
//求表中的个数
public int length() {
return size+1;
}
//返回第i个元素
public E getElem(int i) {
if (i <= size) {
return data[i];
}
return null;
}
// 返回第一个elem的位子
public int locateElem(E elem) {
for (int i = 0; i < size; i++) {
if (elem.equals(data[i])) {
return i;
}
}
return -1;
}
// 返回前驱元素
public E priorElem(E elem) {
int locate = locateElem(elem);
if (locate == 0) {
return null;
} else {
return data[locate - 1];
}
}
// 返回后继元素
public E nextElem(E elem) {
int locate = locateElem(elem);
if (locate == size) {
return null;
} else {
return data[locate + 1];
}
}
// 插入元素,在顺序连表最后插入
public void insertElem(E elem) {
size = size + 1;
if (size >data.length) {
E[] tempdata = (E[]) new Object[data.length + 5];
int j = 0;
for (int i = 0; i < data.length; i++) {
tempdata[j++] = data[i];
}
data = tempdata;
}
data[size] = elem;
}
// 在第i个元素之前插入元素
public void insertElem(int k, E elem) {
size = size + 1;
if (size >= data.length) {
E[] tempdata = (E[]) new Object[data.length + 5];
int j = 0;
// 把元素复制到新数组中
for (int i = 0; i < data.length; i++) {
tempdata[j++] = data[i];
}
data = tempdata;
}
for (int m = size; m >k; m--) {
data[m] = data[m-1];
}
data[k] = elem;
}
//删除第k个元素
public E deleteElem(int k){
E elem;
if(k>=0&&k<data.length){
elem=data[k];
for(int i=k;i<size;i++){
data[i]=data[i+1];
}
size--;
return elem;
}
return null;
}
public String toString() {
String s="[";
for(int i=0;i<size;i++){
s=s+data[i]+",";
}
s=s+data[size]+"]";
return s;
}
}
private int size=-1;
private E[] data=(E[])new Object[10];;
//判断是否为空
public boolean listEmpty() {
return size == 0;
}
//求表中的个数
public int length() {
return size+1;
}
//返回第i个元素
public E getElem(int i) {
if (i <= size) {
return data[i];
}
return null;
}
// 返回第一个elem的位子
public int locateElem(E elem) {
for (int i = 0; i < size; i++) {
if (elem.equals(data[i])) {
return i;
}
}
return -1;
}
// 返回前驱元素
public E priorElem(E elem) {
int locate = locateElem(elem);
if (locate == 0) {
return null;
} else {
return data[locate - 1];
}
}
// 返回后继元素
public E nextElem(E elem) {
int locate = locateElem(elem);
if (locate == size) {
return null;
} else {
return data[locate + 1];
}
}
// 插入元素,在顺序连表最后插入
public void insertElem(E elem) {
size = size + 1;
if (size >data.length) {
E[] tempdata = (E[]) new Object[data.length + 5];
int j = 0;
for (int i = 0; i < data.length; i++) {
tempdata[j++] = data[i];
}
data = tempdata;
}
data[size] = elem;
}
// 在第i个元素之前插入元素
public void insertElem(int k, E elem) {
size = size + 1;
if (size >= data.length) {
E[] tempdata = (E[]) new Object[data.length + 5];
int j = 0;
// 把元素复制到新数组中
for (int i = 0; i < data.length; i++) {
tempdata[j++] = data[i];
}
data = tempdata;
}
for (int m = size; m >k; m--) {
data[m] = data[m-1];
}
data[k] = elem;
}
//删除第k个元素
public E deleteElem(int k){
E elem;
if(k>=0&&k<data.length){
elem=data[k];
for(int i=k;i<size;i++){
data[i]=data[i+1];
}
size--;
return elem;
}
return null;
}
public String toString() {
String s="[";
for(int i=0;i<size;i++){
s=s+data[i]+",";
}
s=s+data[size]+"]";
return s;
}
}