线性表的顺序存储实现
线性表的顺序存储实现是 利用数组的连续存储空间顺序存放线性表的各元素
上代码
上代码
package unit2.section1.demo2;
import java.io.IOException;
public class MyLinearList1 {
private Integer[] data;
private int last;
private int maxSize;
//初始化
public MyLinearList1(int size) {
data=new Integer[size];
this.last=-1;
maxSize=size;
}
//查找
public int find(int value) throws Exception{
int i=0;
while(i<=last&&data[i]!=value) {
i++;
}
if(i>last) {
throw new IOException("没有查到该元素");
}
else {
return i;
}
}
//插入
public void insert(int i,int newValue) throws Exception{
int j;
if(last+1==maxSize) {
throw new IOException("表满");
}
if(i<1||i>last+2) {
throw new IOException("插入位置错误");
}
for(j=last;j>=i-1;j--) {
data[j+1]=data[j];
}
data[i-1]=newValue;
last++;
}
//删除
public void delete(int i) throws Exception{
int j;
if(i<1||i>last+1) {
throw new IOException("不存在第"+i+"个元素");
}
for(j=i;j<=last;j++) {
//注意:此处如果用j=i-1;则结尾也应是last-1;
data[j-1]=data[j];
}
last--;
}
}
线性表的链式存储实现
不要求逻辑上相邻的两个元素物理上也相邻;通过"链"建立起数据元素之间的逻辑关系
插入不需要移动数据元素,只需要修改“链”
上代码
package unit2.section1.demo2;
public class Node {
private Object data;
private Node next;
public Node() {
}
public Node(Node next,Object data) {
this.data=data;
this.next=next;
}
public Node(Object data) {
this.data=data;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
package unit2.section1.demo2;
import java.io.IOException;
public class MyLinearList2 {
public MyLinearList2() {
super();
}
//求表长
public int length(Node sourceNode) {
Node p=sourceNode;
int j=0;
while(p!=null) {
p=p.getNext();
j++;
}
return j;
}
//查找
public Node findKth(Node sourceNode,int k) {
Node p=sourceNode;
int i=1;
while(p!=null&&i<k) {
p=p.getNext();
i++;
}
if(i==k) {
return p;
}else {
return null;
}
}
public Node find(Object x,Node sourceNode) {
Node p=sourceNode;
while(p!=null&&p.getData()!=x) {
p=p.getNext();
}
return p;
}
//插入
public Node insert(Object x,int i,Node sourceNode) throws Exception {
Node p,s;
if(i==1) {
s=new Node();
s.setData(x);
s.setNext(sourceNode);
return s;
}
p=findKth(sourceNode, i-1);
if(p==null) {
throw new IOException("参数错误");
}
else {
s=new Node();
s.setData(x);
s.setNext(p.getNext());
p.setNext(s);
return sourceNode;
}
}
//删除
public Node delete(int i,Node sourceNode) throws IOException {
Node p,s;
if(i==1) {
if(sourceNode!=null) {
sourceNode=sourceNode.getNext();
}else {
return null;
}
return sourceNode;
}else {
p=findKth(sourceNode, i-1);
if(p==null) {
throw new IOException("参数错误");
}else {
s=p.getNext();
p.setNext(s.getNext());
return sourceNode;
}
}
}
private void print(Node node) {
while (node != null) {
System.out.print(node.getData() + "\t");
node = node.getNext();
}
System.out.println();
}
public static void main(String[] args) throws Exception {
MyLinearList2 linearList = new MyLinearList2();
// 作头结点使用
Node head = new Node( null,"唐僧");
// 插入一个结点,位序是1
Node insert1 = linearList.insert( "孙悟空",1, head);
linearList.print(insert1);
// 插入一个结点,位序非1
Node insert2 = linearList.insert( "猪八戒",2,insert1);
linearList.print(insert2);
// 打印链表的长
System.out.println(linearList.length(insert2));
// 按序号查找
Node kth = linearList.findKth(insert2,2);
System.out.println(kth.getData());
// 按值查找
Node find = linearList.find("唐僧", insert2);
System.out.println(find.getData());
// 删除指定位序的结点,比如删除第二个结点
Node delete = linearList.delete(2, insert2);
linearList.print(delete);
}
}