6-2 xcu-sjjg-java-List-2 顺序表就地逆序
将顺序表的元素就地逆置。空间复杂度要求O(1)。
函数接口定义:
方法接口:
public void reverse( )
裁判测试程序样例:
import java.util.Scanner;
class SqList
{
int [] listElem;
int curLen;
public SqList( ) // 创建n个元素构造的顺序表
{
Scanner sc = new Scanner(System.in);
this.listElem = new int[100];
int n = sc.nextInt();
for(int i = 0 ; i<n ;i++ )
{
this.listElem[i] = sc.nextInt();
}
this.curLen = n;
}
//输出所有元素
public void display()
{
int i;
for( i = 0 ; i <this.curLen ; i++)
{
System.out.print( this.listElem[i]+" ");
}
}
@@@
/* 请在这里填写答案 */
}
public class Main{
public static void main(String args[]){
SqList L = new SqList( );
L.reverse();
L.display();
}
}
输入样例:
输入n+1个整数 ,第1个表示 线性表长度,接下来的n个数表示 线性表的元素。
5 1 2 3 4 5
结尾无空行
输出样例:
5 4 3 2 1
结尾无空行
public void reverse( ){
for(int i=0;i<(curLen/2);i++){
int temp=listElem[i];
listElem[i]=listElem[curLen-i-1];
listElem[curLen-i-1]=temp;
}
}