日撸 Java 三百行: DAY12 顺序表2

0.主题

今天在昨天的SequentialList类中增加以下方法:

  • indexOf:给出一个值,若在线性表中存在该值,则返回其首次出现的下标,否则返回-1
  • insert:给出一个值和一个位置,若线性表未满且插入位置合法,则将其插入给定位置,否则插入失败
  • delete:给出一个位置,若该位置合法,则删除该位置上的元素,否则删除失败

1.程序

1.indexOf
遍历线性表,找到给出的值,则返回其下标。遍历完毕仍未找到则说明表中不存在该值,返回-1。
代码如下:

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

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
2.insert
若表已满,则显示信息List full,返回false表示插入失败。
若插入位置非法,则显示信息The position is out of bounds,返回false表示插入失败。
否则将线性表中paraPosition及之后位置上的元素,从后往前依次向后挪一个位置,再将值插入位置paraPosition,并修改表长,返回true表示插入成功。
代码如下:

	/**
	 *****************
	 * Insert a value to a position. If the list is already full, do nothing.
	 * 
	 * @param paraPosition	The given position/
	 * @param paraValue		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
		
		for( int i = length; i > paraPosition; i-- ) {
			data[ i ] = data[ i - 1 ];
		} // Of for i
		
		data[ paraPosition ] = paraValue;
		length++;
		
		return true;
	} // Of insert

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
3.delete
若给定位置非法,则显示信息The position is out of bounds,返回false表示删除失败。
否则将paraPosition之后位置上的元素从前往后依次向前挪一个位置,修改表长,返回true表示删除成功
代码如下:

	/**
	 *****************
	 * 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

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
4.测试
代码如下:

	/**
	 *****************
	 * 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("After initialization, the list is: " + tempFirstList.toString());
	   	System.out.println("Again, the list is: " + tempFirstList);

	   	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);

	   	tempPosition = 2;
	   	tempValue = 5;
	   	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);

	   	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

结果如下:
在这里插入图片描述

2.其他

1.顺序表不是单纯的数组,除了数组外,它还有一个成员变量length,因此插入、删除操作时一定要记得最后需要修改length。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值