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