1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
public class SeqList {
private static final int MAX_SIZE = 1024; private final int[] lis = new int[MAX_SIZE]; private int size;
public SeqList() { size = 0; }
public boolean insert(int data, int position) { if (position >= MAX_SIZE || position < 0) { System.out.println("位置参数错误"); return false; } if (size >= MAX_SIZE) { System.out.println("位置已满"); return false; } for (int i = size; i > position; i--) { lis[i] = lis[i - 1]; } lis[position] = data; size++; return true; }
public boolean delete(int position) { if (position > size || position < 0) { System.out.println("位置参数错误"); return false; }
for (int i = position; i < size; i++) { lis[i] = lis[i + 1]; } size--; return true; }
public int get(int position) { if (position > size || position < 0) { System.out.println("位置参数错误"); return -1; } return lis[position]; }
}
|