Java顺序表元素的插入和删除

一.算法思路

插入算法:

第一步:第一判断数组是否满了,满了你还捣鼓啥呀!第二判断元素位置是不是合法的,你不能插入在小于0 或者 大于数组长度的位置上。对吧!如果不合法,我们就直接return false。并给出相应的提示信息

		if (length == MAX_LENGTH) {
			System.out.println("List is full.");
			return false;
		} // 判断数组是否满载

		if ((paraPosition < 0) || (paraPosition > length)) {
			System.out.println("The position" + paraPosition + "is out of the bounds.");
			return false;
		}//判断位置是否合法

第二步:位置合法,一定要将插入位置和其后的元素往后移一个位置,再把元素赋值给插入的位置,最后注意长度要加一。顺序反了,你弄进去元素就会覆盖当前的数据,往后移动,就是等于在赋值一个插入的元素,而原来位置上的元素就消失了。

		for (int i = length; i > paraPosition; i--) {
			data[i] = data[i - 1];
		} // 元素后移

		data[paraPosition] = paraValue; // 插入元素
		length++;
		return true; 

删除算法:

第一步:这里需要判断数组长度是否为空,没有元素你删个啥呀?判断删除位置是否合法,删除的位置你总得在你的元素存在的范围类吧。

		if (length == 0) {
			System.out.println("Failed  to delete data as list is null.");
			return false;
		} //顺序表不为空

		if ((paraPosition < 0) || (paraPosition > length)) {
			System.out.println("The position " + paraPosition + "is out of the bounds.");
			return false;
		} // 判断插入位置

第二步:删除就是覆盖,直接把删除位置(不包括删除位置)后面的元素往前移动一个。最后别忘了顺序表长度减一。

		for (int i = paraPosition; i < length - 1; i++) {
			data[i] = data[i + 1];
		} // 元素前移一个

		length--; //长度减一
		return true;

兔兔讲堂下课:

完整代码如下,不懂构造顺序表,请点这里。欢迎留言交流

/**
 * 
 */
package datastructure.list;

import javax.imageio.plugins.tiff.FaxTIFFTagSet;

import basic.student;

/**
 ***************************
 * TODO Sequential list
 * 
 * @author Chen Fan
 * @version 1.0 time 2021年12月19日
 ****************************
 */
public class SequentialList {
	/**
	 * The maximal of the list. It is a constant.
	 */
	public static final int MAX_LENGTH = 10;
	/**
	 * The actual Length not exceeding MAX_LENGTH.Attention: Length is not only the
	 * member variable of Sequential list, but also the member variable of Array. In
	 * fact, a name can be the member of different class.
	 */
	int length;
	/**
	 * The data stored in an array.
	 */
	int[] data;

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

	/**
	 *********************
	 * Construct a sequential list using an array.
	 * 
	 * @param paraArray The given array. Its length should not exceed MAX_LENGTH.
	 *                  For simplicity now we do not check it.
	 *********************
	 */
	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 the method claimed in Object, the superclass of any class
	 */
	public String toString() {
		String resultString = "";

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

		resultString += data[length - 1];

		return resultString;
	}// Of toString

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

	/**
	 * 
	 *********************
	 * TODO Find the index of the given value. If it appears in multiple position
	 * simply return the first one.
	 * 
	 * @param paraValue
	 * @return int tempPosition. -1 for not found.
	 *********************
	 */
	public int indexOf(int paraValue) {
		int tempPosition = -1;
		for (int i = 0; i < length; i++) {
			if (data[i] == paraValue) {
				tempPosition = i;
				break;
			} // Of if
		} // Of for i
		return tempPosition;
	}// Of indexOf

	/**
	 * 
	 *********************
	 * TODO Insert a value to a position. If the list is already full, do nothing.
	 * 
	 * @param paraPosiont
	 * @param paraValue
	 * @return Success or not.
	 *********************
	 */
	public boolean insert(int paraPosition, int paraValue) {
		if (length == MAX_LENGTH) {
			System.out.println("List is full.");
			return false;
		} // Of if

		if ((paraPosition < 0) || (paraPosition > length)) {
			System.out.println("The position" + paraPosition + "is out of the bounds.");
			return false;
		}

		// From tail to head. The Last One is moved to a new position.because length <
		// MAX_LENGTH, no exceeding occurs.
		for (int i = length; i > paraPosition; i--) {
			data[i] = data[i - 1];
		} // Of for i

		data[paraPosition] = paraValue;
		length++;
		return true;
	}// Of insert

	/**
	 * 
	 *********************
	 * TODO Delete a value at a position.
	 * 
	 * @param paraPosition
	 * @return Success or not.
	 *********************
	 */
	public boolean delete(int paraPosition) {
		if (length == 0) {
			System.out.println("Failed  to delete data as list is null.");
			return false;
		} //顺序表不为空

		if ((paraPosition < 0) || (paraPosition > length)) {
			System.out.println("The position " + paraPosition + "is out of the bounds.");
			return false;
		} // 判断插入位置

		// From head to tail.
		for (int i = paraPosition; i < length - 1; i++) {
			data[i] = data[i + 1];
		} // 元素前移一个

		length--; //长度减一
		return true;
	}// Of delete

	/**
	 * 
	 *********************
	 * TODO The entrance of the program
	 * 
	 * @param args
	 *********************
	 */
	public static void main(String args[]) {
		int[] tempArray = { 1, 4, 6, 9 };
		SequentialList tempFirstList = new SequentialList(tempArray);
		System.out.println("Intitialized, the list is : " + tempFirstList.toString());
		System.out.println("Again, the list is: " + tempFirstList);

		int tempValue = 4;
		int tempPosition = tempFirstList.indexOf(tempValue);// Get the index of the 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);

		tempPosition = 2;
		tempValue = 5;
		tempFirstList.insert(tempPosition, tempValue);
		System.out.println("After inserting at position" + tempValue + " to position" + tempPosition + ", the list is: "
				+ tempFirstList);

		tempPosition = 8;
		tempValue = 10;
		tempFirstList.insert(tempPosition, tempValue);
		System.out.println("After inserting at position" + tempValue + " to position" + tempPosition + ", the list is: "
				+ tempFirstList);

		tempPosition = 3;
		tempFirstList.delete(tempPosition);
		System.out.println("After deleting data at position " + tempValue + " to position" + tempPosition
				+ ", the list is: " + tempFirstList);

		for (int i = 0; i < 8; i++) {
			tempFirstList.insert(i, i);
			System.out.println("After inserting at position " + i + " to position" + tempPosition + ", the list is: "
					+ tempFirstList);
		}

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

	}// Of main
}// Of class SequentialList.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值