Java线性数据结构(一)
文章目录
前言
在《数据结构》中, 使用“抽象数据类型”来描述不同的数据结构.。在《面向对象程序设计》中, 用对象来存储数据及其上的操作。 我认为, 它们的本质都是相同的。
一、顺序表(一)
1. 对象
对象是数据及其上操作的总和。
例如,我是一个对象, 具有身高、体重、年龄、跑步速度等数据; 同时,我具有吃饭、睡觉、送快递等功能。
从计算机的发展来看,
第一阶段以操作 (函数) 为中心,一个计算导弹轨迹的函数, 根据不同输入获得不同输出。
第二阶段以数据为中心,即数据存放于数据库,使用不同的算法来处理它。第三阶段认为数据及其上的操作是统一不可分的,这就到了面向对象。
2. 类
前面已经使用过 int i; 这类代码, int 就是类型, i 是一个具体的整数变量。同理,对象就是属于某种类的变量.。也可以用集合的方式来理解: 类是集合, 对象是其中的元素; int 是指所有整数的集合, i 是其中的一个元素。
3. 包
包并非程序设计必须的东西, 其作用仅仅是将类进行合理的组织。但是, 在计算机界,往往这种可有可无的东西才是最重要的。如文档、注释、编码规范。可有可无是针对程序的运行而言, 其核心是计算机; 而重要是针对程序的易读性、可维护性而言, 其核心是程序员。
4. final
常量用 final 修饰。这里故意把 MAX_LENGTH 设置得比较少, 方便调拭后面的越界检查代码。
5.new
new生成新的对象。
SequentialList tempFirstList = new SequentialList(tempArray);
6. length
有一个成员变量叫做 length。程序里还有用 length 表示一个整数数组的长度。
实际上, 同一个变量名可以被不同的类所使用, 例如: 人有体重,西瓜也有重量。由于限定了不同的类、不同的对象, 它们之间就不会有冲突。张三的体重、李四的体重,有关联才奇怪了。
这段描述写出来怪怪的, 明明现实生活中就是如此。
但这也正是体现了面向对象的特点: 比面向过程的程序设计更贴合我们的人类认知,也就更远离机器底层。
7. toString
toString这个方法很特殊, 它覆盖了 Object 类的相应方法。 可以看到, 在 println 里面使用 tempFirstList 里, 由于是用另一个字符串与其相加, 系统会自动调用 tempFirstList.toString()。
题目: 使用数组构造一个顺序列表
输入:
一个数组
示例
1, 4, 6, 9
输出:
顺序列表
示例
1, 4, 6, 9
代码如下:
package datastructure.list;
/**
*
* @author Ling Lin E-mail:linling0.0@foxmail.com
*
* @version 创建时间:2022年4月9日 下午6:54:57
*
*/
public class SequentialList {
/**
*
* The maximal length of the list. It is a constant.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length not exceeding MAX_LENGTH. Attention: length is not only
* the member variable of Sequential list, but also the member variable of
* Array. In fact, a name can be the member variable of different classes.
*/
int length;
// The data stored in an array.
int[] data;
/**
* Construct an empty sequential list.
*/
public SequentialList() {
length = 0;
data = new int[MAX_LENGTH];
}// Of the first constructor
/**
* Construct a sequential list using an array.
*
* @param paraArray
*
* The given array. Its length should not exceed MAX_LENGTH. For
* simplicity now we do not check it.
*/
public SequentialList(int[] paraArray) {
data = new int[MAX_LENGTH];
length = paraArray.length;
// Copy data.
for (int i = 0; i < paraArray.length; i++) {
data[i] = paraArray[i];
} // Of for i
}// Of the second constructor
/**
* Overrides the method claimed in Object, the superclass of any class.
*/
@Override
public String toString() {
String resultString = "";
if (length == 0) {
return "empty";
} // Of if
for (int i = 0; i < length - 1; i++) {
resultString += data[i] + ", ";
} // Of for i
resultString += data[length - 1];
return resultString;
}// Of toString
/**
* Reset to empty.
*/
public void reset() {
length = 0;
}// Of reset
/**
* The entrance of the program.
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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);
tempFirstList.reset();
System.out.println("After reset,the list is: " + tempFirstList);
}// Of main
}// Of class SequentialList
运行结果:
二、顺序表(二)
在顺序表(一)的基础上增加。
public static void main(String[] args) {
// TODO Auto-generated method stub
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);
// tempFirstList.reset();
System.out.println("After reset,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
题目一:查找给定元素所处的位置
要求:如果出现在多个位置,返回第一个位置,如果找不到就返回-1.
输入:
一个数
示例:
4
输出:
这个数的位置
示例:
The position of 4 is 1
操作如下:
/**
*
* Find the index of the given value. If it appears in multiple positions,
* simply return the fisrt one.
*
* @param paraValue
* The given value.
* @return The position. -1 for not found.
*/
public int indexOf(int paraValue) {
int tempPosition = -1;
for (int i = 0; i < length; i++) {
if (data[i] == paraValue) {
tempPosition = i;
break;
} // Of if
} // Of for i
return tempPosition;
}// Of indexOf
}// Of class SequentialList
题目二: 在给定位置增加元素
要求:如果线性表已满,或位置不在已有位置范围之内, 就拒绝增加。 该位置可以是在最后一个元素之后一个。
输入:
给定数、给定位置
示例:
5 2
或
10 8
输出:
在给定位置插入给定数之后的顺序表
After inserting 5 to position 2, the list is: 1, 4, 5, 6, 9
或
The position 8 is out of bounds.
或
List full.
操作如下:
/**
* Insert a value to a position. If the list is already full, do nothig.
*
* @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
// From tail to head. The last one is moved to a new position.
// Because length < MAX_LENGTH, no exceeding occurs.
for (int i = length; i > paraPosition; i--) {
data[i] = data[i - 1];
} // Of for i
data[paraPosition] = paraValue;
length++;
return true;
}// of insert
题目三:删除定定位置的元素
要求:要处理给定位置不合法的情况,且该位置必须是已经有数据的。
输入:
一个数,表示要删除的数的位置
示例
3
输出:
删除之后的顺序表
After deleting data at position 3, the list is: 1, 4, 5, 9
操作如下:
/**
* Delete a Value at a position.
*
* @param paraPostion
* 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 bound.");
return false;
} // Of if
// From head to tail.
for (int i = paraPosition; i < length - 1; i++) {
data[i] = data[i + 1];
} // Of for i
length--;
return true;
}// Of delete
运行结果如下: