12.1 查找给定元素所处的位置. 找不到就返回 -1.
12.2 在给定位置增加元素. 如果线性表已满, 或位置不在已有位置范围之内, 就拒绝增加. 该位置可以是在最后一个元素之后一个.
12.3 删除定定位置的元素. 要处理给定位置不合法的情况. 该位置必须是已经有数据的.
12.4 函数 要求同样的输入参数获得同样的输出结果, 但 方法 所依赖的数据既包括参数列表中给出的,也依赖于对象的成员变量. 因此, 面向对象所涉及的参数列表要短些. 例如, locate 方法就有效利用了 length 和 data 这两个成员变量.
package linear_data_structure;
public class SequentialList {
public static final int MAX_LENGTH = 10;
//The maximal length of the list.It is a constant.
int length;
int[] data;
public SequentialList() {
length = 0;
data = new int[MAX_LENGTH];
}
public SequentialList(int[] paraArry) {
data = new int[MAX_LENGTH];
length = paraArry.length;
for (int i = 0; i < paraArry.length; i++) {
data[i] = paraArry[i];
}
}
public String toString() {
String resultString = "";
if (length == 0) {
return "empty";
}
for (int i = 0; i < length - 1; i++) {
resultString += data[i] + ", ";
}
resultString += data[length - 1];
return resultString;
}
public void reset() {
length = 0;
}
//find the index of the given value . If it appears in mutiple positions,
//simply return the first one.
public int indexof(int paraValue) {
int tempPosition = -1;
for (int i = 0; i < length; i++) {
if (data[i] == paraValue) {
tempPosition = i;
break;
}
}
return tempPosition;
}
/*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;
}
if (paraPosition < 0 || paraPosition > length) {
System.out.println("The position" + paraPosition + "is out of bounds");
return false;
}
for (int i = length; i > paraPosition; i--) {
data[i] = data[i - 1];
}
data[paraPosition] = paraValue;
length++;
return 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;
}
for (int i = paraPosition; i < length - 1; i++) {
data[i] = data[i + 1];
}
length--;
return true;
}
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.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 = 2;
tempValue = 5;
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
}
学习了基础的查找,插入,删除操作,内容比较简单,不做过多说明,注意对列表的操作即可。
package linear_data_structure;
public class LinkedList {
class Node {
int data;
Node next;
public Node(int paraValue) {
data = paraValue;
next = null;
}
}
Node header;
public LinkedList() {
header = new Node(0);
}
public String toString() {
String resultString = "";
if (header.next == null) {
return "empty";
}
Node tempNode = header.next;
while (tempNode.next != null) {
resultString += tempNode.data + ", ";
tempNode = tempNode.next;
}
resultString+=tempNode.data;
return resultString;
}
public void reset() {
header.next = null;
}
public int locate(int paraValue) {
int tempPosition = -1;
Node tempNode = header.next;
int tempCurrentPosition = 0;
while (tempNode != null) {
if (tempNode.data == paraValue) {
tempPosition = tempCurrentPosition;
break;
}
tempNode = tempNode.next;
tempCurrentPosition++;
}
return tempPosition;
}
public boolean insert(int paraPosition, int paraValue) {
Node tempNode = header;
Node tempNewNode;
for (int i = 0; i < paraPosition; i++) {
if (tempNode.next == null) {
System.out.println("The position " + paraPosition + " is illeagal.");
return false;
}
tempNode = tempNode.next;
}
//Construct a new node.
tempNewNode = new Node(paraValue);
//Link them
tempNewNode.next = tempNode.next;
tempNode.next = tempNewNode;
return true;
}
public boolean delete(int paraPosition) {
if (header.next == null) {
System.out.println("Cannot delete element from an empty list.");
return false;
} // Of if
Node tempNode = header;
for (int i = 0; i < paraPosition; i++) {
if (tempNode.next.next == null) {
System.out.println("The position " + paraPosition + " is illeagal.");
return false;
}
tempNode = tempNode.next;
}
tempNode.next = tempNode.next.next;
return true;
}
public static void main(String[] args) {
LinkedList tempFirstList = new LinkedList();
System.out.println("Initialized, the list is: " + tempFirstList.toString());
for (int i = 0; i < 5; i++) {
tempFirstList.insert(0, i);
} // Of for i
System.out.println("Inserted, the list is: " + tempFirstList.toString());
tempFirstList.insert(5, 9);
System.out.println("Inserted, the list is: " + tempFirstList.toString());
tempFirstList.delete(4);
tempFirstList.delete(2);
System.out.println("Deleted, the list is: " + tempFirstList.toString());
tempFirstList.delete(0);
System.out.println("Deleted, the list is: " + tempFirstList.toString());
for (int i = 0; i < 5; i++) {
tempFirstList.delete(0);
System.out.println("Looped delete, the list is: " + tempFirstList.toString());
}
}
//insert a value to a position.
}
链表的增删改查,最后的next。next把我搞晕了,再想想吧,想明白,再来改。