栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

0.主题

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

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

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

	
	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表示插入成功。
代码如下:

	
	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表示删除成功
代码如下:

	
	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.测试
代码如下:

	
	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。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/825285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号