Java實現線性表的順序存儲
1.基本思路
1.基本數據的算法實現基本包括四個最重要的功能:增、刪、改、查
2.在這個例子中,我是用數組來實現線性表的順序存儲。如果數組空間不夠,那就要擴大數組。
3.線性表在數組內進行存儲,主要參數有三個:數組arrs,數組長度maxSize,線性表List長度size
2.實際功能列表
3.代碼塊
3.1要點
3.1.1擴大數組:
首先要創建一個更大的數組,然後把arrs通過中介數組oldarrs傳遞到新的大數組newarrs裏面去。
if ((size == maxSize)){
//Expand the size
int[] oldarrs;
int[] newarrs;
oldarrs = arrs;
newarrs = new int[maxSize * 2];
for (int j = 0; j < size; j++) {
newarrs[j] = oldarrs[j];
}
maxSize = maxSize * 2;
arrs = newarrs;
}
3.1.2插入功能(增)
1.如果線性表的size已滿(size == maxSize),就要擴大數組(詳見3.1.1)
2.判斷i是否超出範圍((i < 1) || (i > (size+1)))
3.分兩種情況進行插入:插入中部和插入尾部
public void add(int i, int e){
if ( (i < 1) || (i > (size+1)) ){
throw new IllegalArgumentException(" The i is out of bound!");
}
if ((size == maxSize)){
//Expand the size
int[] oldarrs;
int[] newarrs;
oldarrs = arrs;
newarrs = new int[maxSize * 2];
for (int j = 0; j < size; j++) {
newarrs[j] = oldarrs[j];
}
maxSize = maxSize * 2;
arrs = newarrs;
}
if (i <= size){
for (int j = size - 1; j >= i - 1; j--) {
arrs[j + 1] = arrs[j];
}
}
arrs[i-1] = e;
size++;
display();
}
3.1.3刪除功能(刪):
1.判斷線性表是否爲空(size == 0)
2.判斷i是否超出範圍(i < 1 || i > size)
3.分兩種情況進行刪除:刪除中部和刪除尾部
public void remove(int i){
if (size == 0){
throw new IllegalArgumentException(" Sorry, the List is null!");
}
if (i < 1 || i > size){
throw new IllegalArgumentException(" Sorry, the index is out of bound!");
}
if (i < size){
for (int j = i; j < size; j++) {
arrs[j - 1] = arrs[j];
}
}
size--;
display();
}
3.2基本功能
public class LinearList {
//Create a new array
private int[] arrs;
//The default size
public static final int DEFAULT_SIZE = 10;
//The array's size
private int maxSize;
//The List size
private int size;
/*
Two method to initialize the List
1.Default Size
2.Using the appointed size
*/
public LinearList(){
this(DEFAULT_SIZE);
}
public LinearList(int size){
maxSize = size;
arrs = new int[maxSize];
}
//Determine if the List is empty
public boolean isEmpty(){
if (size == 0){
System.out.println(" The List is empty!");
return true;
}else {
return false;
}
}
//Get the length of the List
public int getLength(){
return size;
}
//Get the appointed element
public int getElement(int i){
if ((size == 0) || (i < 1) || (i > size)){
throw new IllegalArgumentException(" The i is out of bound!");
}
return arrs[i-1];
}
public void LocateElement(int e){
for (int i = 0; i < size; i++) {
if (arrs[i] == e){
System.out.println(" Existing in the " + " index.");
break;
}
if (size == i+1){
System.out.println(" The element is not existent.");
}
}
}
/*
Insert the element e into the ith filed of the List
1.Determine if the index i is out of bound
2.if the size is out of bound, expanding the List Size
3.Divide two situation when you wanna insert element
->between the List
->into the end of the List
*/
public void add(int i, int e){
if ( (i < 1) || (i > (size+1)) ){
throw new IllegalArgumentException(" The i is out of bound!");
}
if ((size == maxSize)){
//Expand the size
int[] oldarrs;
int[] newarrs;
oldarrs = arrs;
newarrs = new int[maxSize * 2];
for (int j = 0; j < size; j++) {
newarrs[j] = oldarrs[j];
}
maxSize = maxSize * 2;
arrs = newarrs;
}
if (i <= size){
for (int j = size - 1; j >= i - 1; j--) {
arrs[j + 1] = arrs[j];
}
}
arrs[i-1] = e;
size++;
display();
}
/*
Remove the element e from the index i of the List
1.Determine if the index i is out of bound, Determine if the size is 0
2.Remove the element from the List
3.Divide two situation when you wanna remove element
->between the List
->on the end of the List
*/
public void remove(int i){
if (size == 0){
throw new IllegalArgumentException(" Sorry, the List is null!");
}
if (i < 1 || i > size){
throw new IllegalArgumentException(" Sorry, the index is out of bound!");
}
if (i < size){
for (int j = i; j < size; j++) {
arrs[j - 1] = arrs[j];
}
}
size--;
display();
}
//Clear the List
public void removeAll() {
if(arrs != null){
for (int i = 0; i < size; i++) {
arrs[i] = 0;
}
}
size = 0;
System.out.println(" Clear completed!");
display();
}
//display the List
public void display(){
if (arrs != null){
System.out.println("");
for (int i = 0; i < size; i++) {
System.out.print(" " + arrs[i]);
}
}
}
}
3.3測試代碼
public static void main(String[] args) {
LinearList linearList = new LinearList(5);
linearList.add(1,1);
linearList.add(2,2);
linearList.add(1,3);
linearList.add(1,4);
linearList.add(1,5);
linearList.add(1,6);
System.out.println();
linearList.remove(1);
linearList.remove(5);
System.out.println();
System.out.println();
linearList.LocateElement(3);
linearList.LocateElement(2);
System.out.println();
linearList.removeAll();
}
3.4輸出示例
4.線性表順序存儲的優缺點
優點 | 缺點 |
---|---|
無須爲表示表中元素之間的邏輯關係而增加額外的存儲空間 | 插入和刪除操作需要移動大量的元素 |
可以快速地存取表中任一位置的元素 | 當線性表長度變化較大時,難以確定存儲空間的容量 |
. | 造成存儲空間的碎片 |