1、顺序表的初始化
2、顺序表的基本操作:
查找(根据位置查找返回元素;根据元素查找返回位置)
插入
删除
重置
package DataStructures;
//顺序表相关操作
import java.util.*;
class SequentialListOperate {
public static final int MAX_LENGTH = 10;
int length;
int[] data;
/*
* Construct an empty sequential list.
*/
public SequentialListOperate() {
length = 0;
data = new int[MAX_LENGTH];
}
/*
* Construct a sequential list using an array.
*/
public SequentialListOperate(int[] array) {
data = new int[MAX_LENGTH];
length = array.length;
for (int i = 0; i < array.length; i++) {
data[i] = array[i];
}
}
public String toString() {
String resultList = "";
if (length == 0) {
return "empty";
}
for (int i = 0; i < length; i++) {
resultList += data[i];
if (i != length - 1) {
resultList += " , ";
}
}
return resultList;
}
/*
* reset the sequentialList
*/
public void reset() {
length = 0;
}
/*
* find the index of the given value.
*/
public int indexOf(int num) {
int flag = -1;
for (int i = 0; i < length; i++) {
if (data[i] == num) {
flag = i;
return flag;
}
}
return flag;
}
/*
* find the value by giving the position
*/
public int ElemLocate(int location) {
int flag = -1;
if (location < 0 || location >= length) {
flag = -1;
} else {
flag = data[location];
}
return flag;
}
public boolean insertList(int location, int num) {
if (length == MAX_LENGTH) {
System.out.println("List full.");
return false;
}
if (location < 0 || location > length) {
System.out.println("指定位置不再有效范围内");
return false;
}
for (int i = length; i > location; i--) {
data[i] = data[i - 1];
}
data[location] = num;
length++;
return true;
}
public boolean deleteList(int location) {
if (location < 0 || location >= length) {
System.out.println("指定位置不再有效范围内");
return false;
}
for (int i = location; i < length - 1; i++) {
data[i] = data[i + 1];
}
length--;
return true;
}
}
public class Sequential_List {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] a = { 1, 2, 3, 4, 5 };
SequentialListOperate list = new SequentialListOperate(a);
System.out.println("初始化的顺序表:" + list.toString());
System.out.println("最初的顺序表长度:" + list.length);
// indexof
System.out.println("请输入要查找元素:");
int l = in.nextInt();
if (list.indexOf(l) != -1) {
System.out.println("该元素的位置是:" + list.indexOf(l));
} else {
System.out.println("未找到该元素");
}
// ElemLocate
System.out.println("请输入元素位置:");
int position = in.nextInt();
if (list.ElemLocate(position) == -1) {
System.out.println("指定位置超出界限");
} else {
System.out.println("该位置上的元素是:" + list.ElemLocate(position));
}
// Insert
System.out.println("请输入要插入的位置:");
int location = in.nextInt();
System.out.println("请输入要插入的元素:");
int num = in.nextInt();
if (list.insertList(location, num)) {
System.out.println("插入成功!!!");
System.out.println("插入后的顺序表:" + list.toString());
System.out.println("插入后的顺序表长度:" + list.length);
} else {
System.out.println("插入失败!!!");
}
// Delete
System.out.println("请输入要删除的元素位置:");
int location2 = in.nextInt();
if (list.deleteList(location2)) {
System.out.println("删除成功!!!");
System.out.println("删除后的顺序表:" + list);
} else {
System.out.println("删除失败!!!");
}
// Reset
list.reset();
System.out.println("重置后的顺序表:" + list);
}
}