day12

这个程序实现了顺序表的基本操作,包括查找元素位置、在指定位置插入元素和删除元素。查找函数会返回第一个匹配值的位置,插入操作会在指定位置增加元素,删除操作则移除指定位置的元素。程序还包含了一个主函数来演示这些操作的应用。
摘要由CSDN通过智能技术生成

顺序表的插值与删除

	/**
	 * 
	 * Locate the given value. If it appears in multiple positions, simply
	 * return the first one.
	 * 
	 * @param paraValue
	 *            The given value.
	 * @return The position. -1 for not found.
	 * 
	 */

	public int locate(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;
		//返回paraValue值所在位置
	}// Of locate

	/**
	 * 
	 * Insert a value to a position. If the list is already full, do nothing.
	 * 
	 * @param paraPosition
	 *            The given value.
	 * @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

		// From tail to head.
		for (int i = length; i > paraPosition; i--) {
			data[i] = data[i - 1];
			//依次往后挪一个位置,下标是由0开始,因此data[i]可储存i+1个数据
		}// Of for

		data[paraPosition] = paraValue;
		length++;
		//按理应该在for前先进行length++,才能储存i+1位置的值,为什么这里不报错呢?

		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

		// From head to tail.
		for (int i = paraPosition; i < length - 1; i++) {
			data[i] = data[i + 1];
			//向左挪一个位置,data[max]点未被重新赋值,因根本没有进入循环
		}// Of for

		length--;
		//length-1时使最右端值索引不到

		return true;
	}// Of insert

	/**
	 * 
	 * The entrance of the program.
	 * 
	 * @param args
	 *            not used now.
	 * 
	 */
	 public static void main(String args[]){
		 int[] tempArray = { 1, 4, 6, 9 };
		 SequentialList tempFirstList = new SequentialList(tempArray);
		 System.out.println("Initialized, the list is: " + tempFirstList.toString());
		 System.out.println("Again, the list is: " + tempFirstList);
		 
		 int tempValue = 4;
		 int tempPosition = tempFirstList.locate(tempValue);
		 //由于locate是属于对象的一个方法,需要由"."方式使用
		 System.out.println("The position of " + tempValue + " is " + tempPosition);
		 
		 tempValue = 5;
		 tempPosition = tempFirstList.locate(tempValue);
		 System.out.println("The position of " + tempValue + " is " + tempPosition);

		 tempPosition = 2;
		 tempValue = 5;
		 tempFirstList.insert(tempPosition, tempValue);
		 System.out.println("After insering " + 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);
		 
		 tempPosition = 3;
		 tempFirstList.delete(tempPosition);
		 System.out.println("After deleting data at 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);
		 }//Of for i
		 
		 tempFirstList.reset();
		 System.out.println("After reset, the list is: " + tempFirstList);
		 
	 }//Of main

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值