package Xianxingbiao;
//线性表中的顺序表
interface List {//接口
public void insert(int i, Object obj) throws Exception;
public Object delete(int i) throws Exception;
public Object getData(int i) throws Exception;
public int size();
public int locate(Object obj);
public boolean isEmpty();
}
public class SeqList implements List {
Object[] listArray;
int size; //当前线性表元素个数
int maxsize; //线性表能容纳的元素的个数
final int defaultSize = 10; //线性表默认的元素个数
public SeqList() { //数据初始化
maxsize = defaultSize;
size = 0;
listArray = new Object[defaultSize];
}
public SeqList(int size) { //人为的控制大小
maxsize = size; //最大容量
this.size = 0; // 当前大小
listArray = new Object[size];
}
//插入操作
public void insert(int i, Object obj) throws Exception {
if (size == maxsize) {//队伍占满无法插入
throw new Exception("顺序表已满,无法插入!");
}
if (i < 0 || i > size) {
throw new Exception("参数错误");
}
for (int j = size; j > i; j--) {
listArray[j] = listArray[j - 1];
}
listArray[i] = obj;
size++;
}
public Object delete(int i) throws Exception {
if (isEmpty()) {
throw new Exception("顺序表为空无法删除!");
}
if (i < 0 || i > size) {
throw new Exception("参数错误");
}
//画图更好理解
for (int j = i + 1; j <= size - 1; j++) {
listArray[j - 1] = listArray[j];
}
size--;
return listArray[i];
}
public Object getData(int i) throws Exception {
if (i < 0 || i > size) {
throw new Exception("参数错误!");
}
if (!isEmpty()) {
throw new Exception("顺序表为空!");
}
return listArray[i];
}
public int size() {
return size; //因为不存在歧义所以可以返回时不需要this
}
public int locate(Object obj) {
int temp = 0;
for (int i = 0; i < size; i++) {
if (listArray[i] == obj) {
temp = i;
// break; 返回第一个满足条件的位置
} else {
temp = -1;
}
}
if (temp == -1) {
System.out.println("没有搜索到值,结果返回:\"-1\"!");
}
return temp;
}
public boolean isEmpty() {
return this.size == 0;
}
public static void main(String[] args) throws Exception {
SeqList list = new SeqList();
list.insert(0, 5);
list.insert(1, 19);
list.insert(2, 19);
list.isEmpty();
System.out.println(list.delete(3));
System.out.println(list.size());
System.out.println(list.locate(19));
}
}
//线性表中的顺序表
interface List {//接口
public void insert(int i, Object obj) throws Exception;
public Object delete(int i) throws Exception;
public Object getData(int i) throws Exception;
public int size();
public int locate(Object obj);
public boolean isEmpty();
}
public class SeqList implements List {
Object[] listArray;
int size; //当前线性表元素个数
int maxsize; //线性表能容纳的元素的个数
final int defaultSize = 10; //线性表默认的元素个数
public SeqList() { //数据初始化
maxsize = defaultSize;
size = 0;
listArray = new Object[defaultSize];
}
public SeqList(int size) { //人为的控制大小
maxsize = size; //最大容量
this.size = 0; // 当前大小
listArray = new Object[size];
}
//插入操作
public void insert(int i, Object obj) throws Exception {
if (size == maxsize) {//队伍占满无法插入
throw new Exception("顺序表已满,无法插入!");
}
if (i < 0 || i > size) {
throw new Exception("参数错误");
}
for (int j = size; j > i; j--) {
listArray[j] = listArray[j - 1];
}
listArray[i] = obj;
size++;
}
public Object delete(int i) throws Exception {
if (isEmpty()) {
throw new Exception("顺序表为空无法删除!");
}
if (i < 0 || i > size) {
throw new Exception("参数错误");
}
//画图更好理解
for (int j = i + 1; j <= size - 1; j++) {
listArray[j - 1] = listArray[j];
}
size--;
return listArray[i];
}
public Object getData(int i) throws Exception {
if (i < 0 || i > size) {
throw new Exception("参数错误!");
}
if (!isEmpty()) {
throw new Exception("顺序表为空!");
}
return listArray[i];
}
public int size() {
return size; //因为不存在歧义所以可以返回时不需要this
}
public int locate(Object obj) {
int temp = 0;
for (int i = 0; i < size; i++) {
if (listArray[i] == obj) {
temp = i;
// break; 返回第一个满足条件的位置
} else {
temp = -1;
}
}
if (temp == -1) {
System.out.println("没有搜索到值,结果返回:\"-1\"!");
}
return temp;
}
public boolean isEmpty() {
return this.size == 0;
}
public static void main(String[] args) throws Exception {
SeqList list = new SeqList();
list.insert(0, 5);
list.insert(1, 19);
list.insert(2, 19);
list.isEmpty();
System.out.println(list.delete(3));
System.out.println(list.size());
System.out.println(list.locate(19));
}
}