![](https://cos-cdn.shuashuati.com/pipixue-web/2020-1231-2005-12/ti_inject-812ce.png)
假设已经定义有顺序表类SequenceList,该类中定义了表的相关操作方法,试编写程序实现顺序表的就地逆置算法,即在的存储空间将顺序表(a1,a2,a3,...,an)逆置为(an,an-1,...,a1)。 public class SequenceList
{ final int maxSize = 10; private T[] listArray; private int length; //顺序表的初始化 public SequenceList(){ length = 0; //线性表初始化为空 listArray = (T[])new Object[maxSize]; } public SequenceList(int n){ if(n<=0){ System.out.println("error!"); System.exit(1); } length = 0; //线性表初始化为空 listArray = (T[])new Object[n]; } //顺序表的插入 public boolean add(T obj,int pos){ if(pos<1 || pos>length+1){ System.out.println("pos值不合法"); return false; } //顺序表数组空间已满,则重新分配存储空间 if(length == listArray.length ){ T[] p = (T[])new Object[length*2]; for(int i=0;i
=pos;i--){ listArray[i] = listArray[i-1]; } listArray[pos-1] = obj; length++; return true; } //删除顺序表中第pos个位置的元素 public T remove(int pos){ if(isEmpty()){ System.out.println("顺序表为空,无法执行删除操作"); return null; }else{ if(pos<1 || pos>length){ System.out.println("pos值不合法"); return null; } T x = listArray[pos-1]; //取得pos位置的元素 //删除位置后的所有元素往前移一位 for(int i=pos;i<=length;i++){ listArray[i-1]=listArray[i]; } length--; return x; } } //在顺序表中查找元素obj出现的位置 public int find(T obj){ if(isEmpty()){ System.out.println("顺序表为空"); return -1; }else{ for(int i=0;i
length){ System.out.println("pos值不合法"); return null; } return listArray[pos-1]; } } //修改顺序表第pos个位置的元素 public boolean modify(T obj,int pos){ if(isEmpty()){ System.out.println("顺序表为空"); return false; }else{ if(pos<1 || pos>length){ System.out.println("pos值不合法"); return false; } listArray[pos-1] = obj; return true; } } //判断顺序表是否为空 public boolean isEmpty(){ return length==0; } //求顺序表的长度 public int Size(){ return length; } //正序输出顺序表中所有元素 public void nextOrder(){ for(int i=0;i