题目描述
已知顺序表类的定义如下,实现各个成员函数。主函数中输入数据(以0结束)利用Insert函数依次将数据插入到表的1号位置,利用DispList按照逻辑次序输出表中元素,再输入一个要查找的元素,利用查找函数Locate查找其在表中的位置,最后利用Reverse函数将数据逆序,再利用DispList输出。
template
class SeqList{
public:
SeqList(); //构造函数,将表置空
~SeqList(){} //析构
int Locate(T x); //查找元素x在表中的位置,成功返回x在表中的位置,否则返回0
void Insert(int i, T x); //在表的i位置插入元素x
void Reverse(); //逆序表中数据
void DispList();
private:
T data[MaxSize]; //存储元素
int length; //顺序表实际长度
};
输入
输入样例说明:例如输入数据为:1 2 3 4 5 6 0 3,即将1,2,3,4,5,6插入表中的1号位置,得到逻辑次序为6,5,4,3,2,1的顺序表,3为在表中待查找的数据,3的位置为4。
输入:1 2 3 4 5 6 0 3
输出:
The length:6
The elements:
6 5 4 3 2 1
Found position:4
The length:6
The elements:
1 2 3 4 5 6
若查找的数据不存在,则输出:No found,例如下例
输入:1 2 3 4 5 6 0 9
输出:
The length:6
The elements:
6 5 4 3 2 1
No found
The length:6
The elements:
1 2 3 4 5 6
输出
样例输入
样例输出
The length:6
The elements:
6 5 4 3 2 1
Found position:4
The length:6
The elements:
1 2 3 4 5 6
提示
来源
Submit
import java.util.Scanner;class SeqList{private Object[] listElem;private int length ;public SeqList(int maxSize) {length = 0;listElem = new Object[maxSize];}public Object get(int i) throws Exception {if(i<0||i>length -1)throw new Exception("No found");return listElem[i];}public void insert(int i, Object x) throws Exception {if (length == listElem.length)throw new Exception("full");if (i < 0 || i > length )throw new Exception("Invalid insertion position");for (int j = length ; j > i; j--)listElem[j] = listElem[j - 1];listElem[i] = x;length ++;}public void reverse() {for (int i = 0; i < length/2 ; i++) {Object temp = listElem[i];listElem[i] = listElem[length-i-1];listElem[length-i-1] = temp;}}public void display() {System.out.println("The length:"+length );System.out.println("The elements:");for (int j = 0; j < length ; j++) {System.out.print(listElem[j]+ " ");}System.out.println();}}public class Main {public static void main(String[] args) {SeqList L = new SeqList(10);Scanner sc = new Scanner(System.in);while(sc.hasNext()){int d = sc.nextInt();if(d==0)break;try {L.insert(0,d);} catch (Exception e) {e.printStackTrace();}}L.display();try {System.out.println("Found position:"+L.get(sc.nextInt()-1));} catch (Exception e) {System.out.println("No found");}L.reverse();L.display();}}
