一.数据结构之顺序表
二.代码展示
三.总结
一.数据结构之顺序表
1.线性表
提到顺序表,首先要想到线性表。线性表是n个具有相同特性的数据元素的有限序列,是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
2.顺序表
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,使得线性表在逻辑结构上相邻的元素存储在连续的物理存储单元中。一般情况下采用数组存储,在数组上完成数据的增删查改。
顺序表的基本操作
1.初始化
public SequentialList() {
length = 0;
data = new int[MAX_LENGTH];
}//of the first constructor
/*
a constructor with a param which is a given array
@param paraArray
*/
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
构造方法
每个类都有构造方法。如果没有显式地为类定义构造方法,Java 编译器将会为该类提供一个默认构造方法。
在创建一个对象的时候,至少要调用一个构造方法。构造方法的名称必须与类同名,一个类可以有多个构造方法。
关于Java对象和类的解释可看关于Java的构造器.
2. 查找给定元素所处的位置
/*
*find the index of the given value
* @param paraValue
* @return the index ,if nor found,return -1
*/
public int indexOf(int paramValue) {
int tempPostion = -1;
for (int i = 0; i < length; i++) {
if (data[i] == paramValue) {
tempPostion = i;
break;
}//of if
}//of for i
return tempPostion;
}
3.在给定位置增加元素
/*
insert a given value into to given position
@param paramPostion
@param paramValue
@return success or not.
*/``
public boolean insert(int paramPosition, int paramValue) {
if (length == MAX_LENGTH) {
return false;
}//of if
if (paramPosition < 0 || paramPosition > length) {
System.out.println("The position " + paramPosition + "is out of bounds.");
return false;
}//of if
for (int i = length; i > paramPosition; i--) {
data[i] = data[i - 1];// from the tail to the top
}//of for i
data[paramPosition] = paramValue;
length++;
return true;
}//of insert
- 删除定定位置的元素
/*
delete the value on the given position
@param paramPosition the given position
@return success or not.
*/
public boolean delete(int paramPosition) {
if (paramPosition < 0 || paramPosition >= length) {
return false;
}//of if
//from top to the tail
for (int i = paramPosition; i < length - 1; i++) {
data[i] = data[i + 1];//cover the former data
}//of for i
length--;
return true;
}//of delete
遍历过程中:data[i] = data[i + 1];也就是说,不断用 后一个元素 覆盖当前元素,这也就是为什么我们从前向后遍历的原因,因为逆向遍历是无法实现后段向前段覆盖的。
再提一下Java中的关键字final:final 表示"最后的、最终的"含义,变量一旦赋值后,不能被重新赋值。被 final 修饰的实例变量必须显式指定初始值。
final 修饰符通常和 static 修饰符一起使用来创建类常量。按照习惯通常变量名全大写
public static final int MAX_LENGTH = 10;
二.代码展示
package dataStructure.list;
/**
* @author Donghao Xu
*/
public class SequentialList {
/**
* constant
*/
public static final int MAX_LENGTH = 10;
/*
the actual length
*/
int length;
/*
the data stored int an array
*/
int[] data;
/**
* an constructor without param
*/
public SequentialList() {
length = 0;
data = new int[MAX_LENGTH];
}//of the first constructor
/*
a constructor with a param which is a given array
@param paraArray
*/
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 "toString"
*/
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
/*
*find the index of the given value
* @param paraValue
* @return the index ,if nor found,return -1
*/
public int indexOf(int paramValue) {
int tempPostion = -1;
for (int i = 0; i < length; i++) {
if (data[i] == paramValue) {
tempPostion = i;
break;
}//of if
}//of for i
return tempPostion;
}
/*
insert a given value into to given position
@param paramPostion
@param paramValue
@return success or not.
*/
public boolean insert(int paramPosition, int paramValue) {
if (length == MAX_LENGTH) {
return false;
}//of if
if (paramPosition < 0 || paramPosition > length) {
System.out.println("The position " + paramPosition + "is out of bounds.");
return false;
}//of if
for (int i = length; i > paramPosition; i--) {
data[i] = data[i - 1];// from the tail to the top
}//of for i
data[paramPosition] = paramValue;
length++;
return true;
}//of insert
/*
delete the value on the given position
@param paramPosition the given position
@return success or not.
*/
public boolean delete(int paramPosition) {
if (paramPosition < 0 || paramPosition >= length) {
return false;
}//of if
//from top to the tail
for (int i = paramPosition; i < length - 1; i++) {
data[i] = data[i + 1];//cover the former data
}//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 = {1, 4, 6, 9};
SequentialList tempFirstList = new SequentialList(tempArray);
System.out.println("Initialized,the first 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 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("Reset,the list is:" + tempFirstList);
}//of main
}//of class SequentialList
运行结果
Initialized,the first list is:1,4,6,9
Again,the list is:1,4,6,9
The position of 4 is 1
The position of 5 is -1
After inserting 5 to position 2,the list is:1,4,5,6,9
The position 8 is out of bounds.
After inserting 10 to position 8,the list is:1,4,5,6,9
After deleting at position 3,the list is:1,4,5,9
After inserting 0 to position0,the list is:0,1,4,5,9
After inserting 1 to position1,the list is:0,1,1,4,5,9
After inserting 2 to position2,the list is:0,1,2,1,4,5,9
After inserting 3 to position3,the list is:0,1,2,3,1,4,5,9
After inserting 4 to position4,the list is:0,1,2,3,4,1,4,5,9
After inserting 5 to position5,the list is:0,1,2,3,4,5,1,4,5,9
After inserting 6 to position6,the list is:0,1,2,3,4,5,1,4,5,9
After inserting 7 to position7,the list is:0,1,2,3,4,5,1,4,5,9
Reset,the list is:empty
针对这条运行结果:After inserting 10 to position 8,the list is:1,4,5,6,9
发现顺序表只能按顺序一个一个添加进去,并不像链表的指针离散分配。
链表的定义;链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。(参考百度)关于链表的执行,明天也会写代码来运行,同时对两种线性结构进行总结。
三.总结
顺序表的实现跟生活实际有关,但是在实现插入和删除的时候,需要移动大量的数组元素,导致效率低,并且使用前需声明数组的长度,一旦声明长度就不能更改。