day12:顺序表二

1、顺序表内查找与给定值匹配的元素位置:
①从头到尾逐个比较:值或者关键字;
②返回值:匹配成功的元素的位序,找不到即返回-1;
③如果出现了多次,只需要返回第一次出现的位置;

public int indexOf(int paraValue) {
	int position = -1;
	for (int i = 0; i < data.length; i++) {
		if (data[i] == paraValue) {
			position = i;
			break;
		} // Of if
	} // Of for i
	return position;
}// Of indexOf

2、顺序表内制定位置插入新元素:
①指定位置及其后的元素:后移一个位置;
②如果顺序表是满的,则不再插入;

如果想了解有关顺序表在数据存储上、基本操作上的特点;顺序表的初始化、清空顺序表等操作,可以看之前这篇文章:https://blog.csdn.net/m0_53096519/article/details/122282660?spm=1001.2014.3001.5501

/**
* At first, judge whether the list is full.
* And then, check the given position.
* Last, insert the given paraValue to the given position.
* @param position         The given position.
* @param paraValue        The given paraValue.
* @return                 Return the true or false.
*/
public boolean insert(int position, int paraValue) {
	if (length == MAX_LENGTH) {
		System.out.println("The list is full!");
		return false;
	}// Of if
	if ((position < 0) || (position > length)) {
		System.out.println("The position" + position + "is out of the list");
		return false;
	}// Of if
	for (int i = length; i > position; i--) {
		data[i] = data[i - 1];
	} // Of for i
	data[position] = paraValue;
	length++;
	return true;
}

3、顺序表内删除指定位置的元素:
指定位置后面的元素:前移一个位置;

/**
* Firstly, check the given position.
* Then, delete the value in the given position.
* @param position   The given position.
* @return           Return the true or false.
*/
public boolean delete(int position) {
	if((position < 0) || (position >= length)) {
		System.out.println("The position" + position + "is out of the list.");
		return false;
	} // Of if
		for (int i = position; i < length - 1; i++) {
		data[i] = data[i + 1];
	} // Of for i
	length--;
	return true;
}// Of delete

综合示例:

package linear_list;

/**
 * The SeguentialList.
 * 
 * @author 前夜
 *
 */
public class SequentialList{
	
	public static final int MAX_LENGTH = 10;
	int length;
	int[] data;
	
	public SequentialList() {
		length = 0;
		data = new int[MAX_LENGTH];
	}// Of SequentialList
	
	public SequentialList(int[] array) {
		data = new int[MAX_LENGTH];
		length = array.length;
		
		for (int i = 0; i < array.length; i++) {
			data[i] = array[i];
		} // Of for i
	}// Of SequentialList
	
	/**
	 * Judge whether the list is empty.
	 * 
	 * Print the elements in the list.
	 */
	public String toString() {
		String resultString = "";
		
		if (length == 0) {
			return "The list is empty!";
		}// Of if
		else if (length != 0) {
			for (int i = 0; i < length - 1; i++) {
				resultString += data[i] + ", ";
			} // Of for i
		}// Of if
		
		resultString += data[length - 1];
		return resultString;
	}// Of toString
	
	/**
	 * Create an empty list.
	 */
	public void reset() {
		length = 0;
	}// Of reset
	
	/**
	 * Return the position of the paraValue.
	 * @param paraValue
	 * @return
	 */
	public int indexOf(int paraValue) {
		int position = -1;
		for (int i = 0; i < data.length; i++) {
			if (data[i] == paraValue) {
				position = i;
				break;
			} // Of if
		} // Of for i
		return position;
	}// Of indexOf
	
	/**
	 * At first, judge whether the list is full.
	 * And then, check the given position.
	 * Last, insert the given paraValue to the given position.
	 * @param position         The given position.
	 * @param paraValue        The given paraValue.
	 * @return                 Return the true or false.
	 */
	public boolean insert(int position, int paraValue) {
		if (length == MAX_LENGTH) {
			System.out.println("The list is full!");
			return false;
		}// Of if
		if ((position < 0) || (position > length)) {
			System.out.println("The position " + position + " is out of the list.");
			return false;
		}// Of if
		for (int i = length; i > position; i--) {
			data[i] = data[i - 1];
		} // Of for i
		data[position] = paraValue;
		length++;
		return true;
	}

	/**
	 * Firstly, check the given position.
	 * Then, delete the value in the given position.
	 * @param position   The given position.
	 * @return           Return the true or false.
	 */
	public boolean delete(int position) {
		if((position < 0) || (position >= length)) {
			System.out.println("The position " + position + " is out of the list.");
			return false;
		} // Of if
		for (int i = position; 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 = { 2, 4, 6, 8 };
		SequentialList firstList = new SequentialList(tempArray);
		System.out.println("Intitailized, the list is: " + firstList.toString());
		System.out.println("Again, the list is: " + firstList);
		
		// Find out the position of the tmpValue.
		int tempValue = 4;
		int position = firstList.indexOf(tempValue);
		System.out.println("The position of " + tempValue + " is " + position);
		
		tempValue = 3;
		position = firstList.indexOf(tempValue);
		System.out.println("The position of " + tempValue + " is " + position);
		
		// Insert the tempValue to the given position.
		tempValue = 5;
		position = 2;
		firstList.insert(position, tempValue);
		System.out.println("After inserting " + tempValue + " to position " + position + ", the list is: " + firstList);
		
		tempValue = 9;
		position = 10;
		firstList.insert(position, tempValue);
		System.out.println("After inserting " + tempValue + " to position " + position + ", the list is: " + firstList);
		
		// Delete the value of the given position.
		position = 4;
		firstList.delete(position);
		System.out.println("After deleting data at position " + position + ", the list is: " + firstList);
		
		for (int i = 0; i < 8; i++) {
			firstList.insert(i, i);
			System.out.println("After inserting " + i + " to position " + i + ", the list is: " + firstList);
		} // Of for i
		
		firstList.reset();
		System.out.println("After reset, the list is: " + firstList);
	}// Of main
}// Of class SequentialList
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值