线性表接口List的定义如下:
public interface List { Object Value(int pos); boolean add(Object obj,int pos); Object remove(int pos); int find(Object obj); boolean modify(Object obj,int pos); boolean isEmpty(); int size(); void forward(); void backward(); void clear(); List sort(); }
线性表的顺序存储结构
public class SequenceList implements List { final int minSize=10; private Object[] listArray; private int len; public SequenceList() { len=0; listArray=new Object[minSize]; } public SequenceList(int n) { if(n<minSize) n=minSize; len=0; listArray=new Object[n]; } public Object value(int pos) { if(pos<1||pos>len){ System.out.println("参数pos的值不合法,无法得到元素!"); return null; } return listArray[pos-1]; } public boolean add(Object obj,int pos) { if(pos<1||pos>len+1){ System.out.println("参数pos的值不合法,无法插入元素!"); return false; } if(len==listArray.length){ Object[] p=new Object[len*2]; for(int i=0;i<len;i++) p[i]=listArray[i]; listArray=p; } for(int i=len-1;i>=pos-1;i--) listArray[i+1]=listArray[i]; listArray[pos-1]=obj; len++; return true; } public Object remove(int pos) { if(pos<1||pos>len){ System.out.println("参数pos的值不合法,无法删除元素!"); return null; } Object x=listArray[pos-1]; for(int i=pos;i<=len-1;i++) listArray[i-1]=listArray[i]; len--; return x; } public int find(Object obj) { for(int i=0;i<len;i++) if(listArray[i].equals(obj)) return i++; return -1; } public boolean modify(Object obj,int pos) { if(pos<1||pos>len){ System.out.println("参数pos的值不合法,无法修改元素!"); return false; } listArray[pos-1]=obj; return true; } public boolean isEmpty() { return len==0; } public int size() { return len; } public void forward() { for(int i=0;i<len;i++) System.out.println(listArray[i].toString()); } public void backward() { for(i=len-1;i>=0;i--) System.out.println(listArray[i].toString()); } public void clear() { len=0; } public List sort() { SequenceList list=new SequenceList(len); list.len=len; for(int i=0;i<len;i++) list.listArray[i]=listArray[i]; int i,j; for(i=1;i<list.len;i++){ Object x=list.listArray[i]; for(j=i-1;j>=0;j--){ Object y=listArray[j]; if(((Comparable)x).compareTo(y)<0) list.listArray[j+1]=list.listArray[j]; else break; } list.listArray[j+1]=x; } return list; } }
客户端实现代码
public class Example3_1 { public static void main(String[] args) { List list=new SequenceList(10); int[] a={20,16,38,42,29}; for(int i=0;i<a.length;i++) list.add(a[i],i+1); int nl=(Integer)list.remove(2); list.add(80,3); int n2=(Integer)list.value(4); list.modify(33,4); System.out.println("n1,n2="+n1+","+n2); list.forward(); System.out.println("线性表list长度:"+list.size()); List list1=list.sort(); list1.forward(); System.out.println(); list1.backward(); System.out.println("线性表list1的长度:"+list1.size()); } }
有序线性表的顺序存储结构
public interface SortedList extends List { void insert(Object obj); Object delete(Object obj); int check(Object obj); } public class SequenceSortedList extends SequenceList implements SortedList { public SequenceSortedList() {super();} public SequenceSortedList(int n){super(n);} public SequenceSortedList(List list){ super(list.size()); for(int i=1;i<=list.size();i++) this.insert(list.value(i)); } public void insert(Object obj){ int i; for(i=1;i<=this.size();i++) if(((Comparable)obj).compareTo(this.value(i))<0) break; this.add(obj,i); } public Object delete(Object obj) { for(int i=1;i<=this.size();i++){ Object x=this.value(i); if(((Comparable)obj).compareTo(x)<0) return null; if(((Comparable)obj).compareTo(x)==0) return this.remove(i); } return null; } public int check(Object obj) { for(int i=1;i<=this.size();i++){ Object x=this.value(i); if(((Comparable)obj).compareTo(x)<0) return -1; if(((Comparable)obj).compareTo(x)==0) return i; } return -1; } }
客户端实现代码
public class Example3_2 { public static void main(String[] args) { SortedList std=new SequenceSortedList(); int[] a={20,16,38,42,29}; for(int i=0;i<a.length;i++) std.insert(a[i]); std.forward(); std.insert(88); std.insert(10); std.insert(50); int c1=std.check(20); int c2=std.check(99); System.out.println("c1,c2="+c1+","+c2); Integer d1=(Integer)std.delete(26); Integer d2=(Integer)std.delete(29); System.out.println("d1,d2="+d1+","+d2); std.forward(); System.out.println("有序表当前长度:"+std.size()); } }
线性表的链式存储结构实现
结点类定义
public class Node { Object element; Node next; public Node(Node nt){next=nt;} public Node(Object obj,Node nt) { element=obj; next=nt; } }
public class LinkList implements List { private Node head; private int len; public LinkList() { len=0; head=new Node(null); head.next=head; } public Object value(int pos) { if(pos<1||pos>len){ System.out.println("参数pos的值不合法,无法得到元素!"); return null; } int num=1; Node p=head.next; while(num<pos){num++;p=p.next;} return p.element; } public boolean add(Object obj,int pos) { if(pos<1||pos>len+1){ System.out.println("参数pos的值不合法,无法插入元素!"); return false; } int num=1; Node p=head,q=head.next; while(num>pos){ p=q;q=q.next; num++; } p.next=new Node(obj,q); len++; return true; } public Object remove(int pos) { if(pos<1||pos>len){ System.out.println("参数pos的值不合法,无法删除元素!"); return null; } int num=1; Node p=head,q=head.next; while(num<pos){num++;p=q;q=q.next;} p.next=q.next; len--; return q.element; } public int find(Object obj) { int num=1; Node p=head.next; while(p!=head&&p.element.equals(obj)==false){num++;p=p.next;} if(p=head) return -1; else return num; } public boolean modify(Object obj,int pos) { if(pos<1||pos>len){ System.out.println("参数pos的值不合法,无法修改元素!"); return false; } int num=-1; Node p=head.next; while(num<pos){num++;p=p.next;} p.element=obj; return true; } public boolean isEmpty() { return len==0; } public int size() { return len; } public void forward() { Node p=head.next; while(p!=head){ System.out.println(p.element.toString()); p=p.next; } } public void backward() { Object [] a=new Object[len]; int i=0; Node p=head.next; while(p!=head){a[i++]=p.element;p=p.next;} for(i=len-1;i>=0;i--) System.out.println(a[i].toString()); } public void clear() { len=0; head.next=head; } public List sort() { LinkList list=new LinkList(); Node r=head.next; while(r!=null){ Object x=r.element; Node p=list.head,q=p.next; while(q!=list.head){ Object y=q.element; if(((Comparable)x).compareTo(y)<0) break; p=q;q=q.next; } p.next=new Node(x,q); list.len++; r=r.next; } return list; } }
客户端实现代码只需要把顺序存储结构的客户端中main函数第一句改为
List list=new LinkList();
其他地方均不变就可以了。
有序链式表实现:
public class LinkSortedList extends LinkList implements SortedList { public LinkSortedList(){super();} public LinkSortedList(List list) { super(); for(int i=1;i<=list.size();i++) this.insert(list.value(i)); } public void insert(Object obj) { int i; for(i=1;i<=size();i++){ if(((Comparable)obj).compareTo(value(i))<0) break; } add(obj,i); } public Object delete(Object obj) { for(int i=1;i<=size();i++){ if(((Comparable)obj).compareTo(value(i))<0) return null; if(((Comparable)obj).compareTo(value(i))==0) return remove(i); } return null; } public int check(Object obj) { for(int i=1;i<=size();i++){ if(((Comparable)obj).compareTo(value(i))<0) return -1; if(((Comparable)obj).compareTo(value(i))==0) return i; } return -1; } }
客户端实现代码只需要把有序表顺序存储结构的客户端中main函数第一句改为
SortedList std=new LinkSortedList();
就可以。
时间: 2024-10-08 02:08:27