说明:没有学习泛型,这里线性表内容没有使用引用类型,使用int类型表示
队列类:
package Class.LinearList;
public class LinearList {
public int length = 0;
private int[] arr;
private static final int MIN_CAPACITY = 3;
// 构造方法一
public LinearList(int length) {
if (length >= MIN_CAPACITY) {
arr = new int[length];
} else {
System.out.println("要求线性表最小长度为3!");
}
}
// 构造方法二
public LinearList() {
this(MIN_CAPACITY);
}
public LinearList(int []arr) {
if(arr.length <= MIN_CAPACITY) {
throw new RuntimeException("队列长度过小!");
}
this.arr = arr;
length = arr.length;
}
//判断线性表是否为空
public boolean isEmpty() {
return length == 0;
}
// 判断线性表是否为满
public boolean isFull() {
return length == arr.length;
}
// 返回线性表长度
public int size() {
return length;
}
// 返回首个与key相等的元素的索引,查找不成功,返回null(这里是0)
public int keySearch(int key) {
if (isEmpty()) {
throw new RuntimeException("线性表为空!");
}
for (int i = 0; i < length; i++) {
if (arr[i] == key) {
return i;
}
}
System.out.println("线性表中没有该元素");
return 0;
}
// 删除首个与key相等的元素的,返回被删除元素
public void keyRemove(int key) {
if (isEmpty()) {
System.out.println("线性表为空!");
return;
}
for (int i = 0; i < length; i++) {
if (arr[i] == key) {
arr[i] = 0;
length--;
return;
}
}
System.out.println("线性表中没有该元素");
}
// 插入value,作为第n个元素
public void insert(int value, int n) {
if (isFull()) {
System.out.println("线性表已满");
return;
}
// 插入位置
if (n >= 1 && n <= length + 1) {
for (int i = length; i >= n; i--) {
arr[i] = arr[i - 1];
}
arr[n - 1] = value;
} else if (n > length + 1) {
System.out.println("插入位置过大,自动插入到尾部");
this.insert(value, length + 1);
} else if (n < 1) {
System.out.println("插入位置过小,自动插入到头部");
this.insert(value, 1);
}
length++;
}
// 在尾部插入value元素
public void tailInsert(int value) {
this.insert(value, length + 1);
}
// 删除第n个元素
public void remove(int n) {
if (isEmpty()) {
throw new RuntimeException("线性表为空!");
}
if (n >= 1 && n <= length) {
for (int i = n - 1; i <= length - 2; i++) {
arr[i] = arr[i + 1];
}
arr[length - 1] = 0;
length--;
} else {
throw new RuntimeException("删除位置有误");
}
}
// 设置第n个元素为value
public void setValue(int value, int n) {
if (isEmpty()) {
System.out.println("当前线性表为空");
}
if (n > length || n < 1) {
System.out.println("访问越界");
return;
} else {
arr[n - 1] = value;
}
}
//
public void showInfo() {
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println("");
}
//
public int getInt(int index) {
return arr[index];
}
}
约瑟夫环:
package Class.Josephus;
import Class.LinearList.LinearList;
public class Josephus_LinearList {
private LinearList linearList;
/*
本约瑟夫环start即起始位置为索引值,可以为0
*/
// 传入队列构造器
public Josephus_LinearList(LinearList linearList) {
this.linearList = linearList;
}
public int getSurvivorInt(int start, int distance) {
if (start < 0 || start > linearList.size() - 1 || distance <= 0) {
throw new RuntimeException("参数输入错误!");
}
while (linearList.length > 1) {
start = (start + distance - 1) % linearList.length;
linearList.remove(start + 1);
}
return linearList.getInt(0);
}
}
class Start_Kill {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
LinearList linearList = new LinearList(arr);
Josephus_LinearList josephusLinearList = new Josephus_LinearList(linearList);
int survivor = josephusLinearList.getSurvivorInt(1, 3);
System.out.println("生存者是" + survivor);
}
}