day12

Day12——Sequential List

1. Background

今天是学习java的第十二天,依旧是对顺序表进行学习。

今天的代码是在昨天的基础上进行增加的,增加了顺序表的,增、删、查操作。

这些操作的实现在数据结构中是非常基础的,几乎在数据结构这门课程中任何新的内容的第一件事就是实现增删改查,所以,就不赘述,直接进入今天的代码。

2. Code

package basic;

public class SequentialList {
    /**
     * The max length of list.
     */
    public static final int MAX_LENGTH = 10;

    int length;

    /**
     * The data stored in the array.
     */
    int[] data;

    /**
     * Construct an empty sequential list.
     */
    public SequentialList() {
        length = 0;
        data = new int[MAX_LENGTH];
    }// Of the SequentialList

    /**
     ******************
     * Overload the method. The length of paraArray must be not exceed MAX_LENGTH.
     * 
     * @param paraArray is the given array.
     ******************
     */
    public SequentialList(int[] paraArray) {
        data = new int[MAX_LENGTH];
        length = paraArray.length;

        // Copy data
        for (int i = 0; i < paraArray.length; i++) {
            data[i] = paraArray[i];
        } // Of for i
    } // Of the second constructor

    /**
     ******************
     * Overrides toString.
     ******************
     */
    public String toString() {
        String resultString = "";

        if (length == 0) {
            return "empty";
        } // Of if

        for (int i = 0; i < length - 1; i++) {
            resultString += data[i] + ", ";
        }

        resultString += data[length - 1];
        return resultString;
    } // Of to String

    /**
     ******************
     * Reset to empty.
     ******************
     */
    public void reset() {
        length = 0;
    } // Of reset

    /**
     *******************
     * Find the index of the given value. 
     * 
     * @param paraValue
     * @return tempPosition, -1 for not found.
     *******************
     */
    public int indexOf(int paraValue) {
        int tempPosition = -1;

        for (int i = 0; i < length; i++) {
            if (paraValue == data[i]) {
                tempPosition = i;
                break;
            } // Of of
        } // Of for i

        return tempPosition;
    } // Of indexOf

    /**
     *******************
     * Insert a velue to the position.
     * 
     * @param paraPosition
     * @param paraValue
     * @return Success or not.
     *******************
     */
    public boolean Insert(int paraPosition, int paraValue) {
        if (length == MAX_LENGTH) {
            System.out.println("List full"); // 此处表示数组长度已达到最大值
            return false;
        } // Of if

        if ((paraPosition < 0) || (paraPosition > length)) { // 判断是否越界
            System.out.println("The position " + paraPosition + " is out of bounds.");
            return false;
        } // Of if

        for (int i = length; i > paraPosition; i--) {
            data[i] = data[i - 1];
        } // Of for i
        data[paraPosition] = paraValue;
        length++;

        return true;
    } // Of Insert

    /**
     *******************
     * Delete a value at a position.
     * 
     * @param paraPosition The given position.
     * @return Success or not.
     * ******************
     */
    public boolean Delete(int paraPosition) {
        if (paraPosition < 0 || paraPosition > length) { // 判断是否越界
            System.out.println("The position " + paraPosition + " is out of bounds.");
            return false;
        } // Of if

        for (int i = paraPosition; i < length - 1; i++) {
            data[i] = data[i + 1];
        } // Of for i

        length--;

        return true;
    } // Of Delete

    /**
     * The entrance of the program.
     * 
     * @param args Not used now.
     */
    public static void main(String[] args) {
        int[] tempArray = { 1, 3, 5, 7, 9 };
        SequentialList tempFirstList = new SequentialList(tempArray);
        System.out.println("Initialized, the list is: " + tempFirstList.toString());
        System.out.println("the list is " + tempFirstList);

        // 检查indexOf方法
        int tempValue = 4; // 给定值数组中已出现
        int tempPosition = tempFirstList.indexOf(tempValue);
        System.out.println("The position of " + tempValue + " is: " + tempPosition);

        tempValue = 5; // 给定值数组中未出现
        tempPosition = tempFirstList.indexOf(tempValue);
        System.out.println("The position of " + tempValue + " is: " + tempPosition);

        // 检查Insert方法
        tempPosition = 2;
        tempValue = 4;
        tempFirstList.Insert(tempPosition, tempValue);
        System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

        tempPosition = 8; // 给的位置超过数组边界
        tempValue = 10;
        tempFirstList.Insert(tempPosition, tempValue);
        System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

        // 检查Delete方法
        tempPosition = 3;
        tempFirstList.Delete(tempPosition);
        System.out.println("After deleting to position " + tempPosition + ", the list is: " + tempFirstList);

        // 循环插入值
        for (int i = 0; i < 8; i++) {
            tempFirstList.Insert(i, i);
            System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);
        }

        tempFirstList.reset();
        System.out.println("After reset, the list is: " + tempFirstList);

    } // Of main

} // Of SequentialList

运行结果:

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值