用链式实现的线性表,公共接口ADT跟上一篇一样
1,有序链表
成员变量和构造函数:
private LinearNode front;
private int count;
public LinkedOrderedList()
{
front = null ;
count = 0 ;
实现清单(将ADT和有序链表扩展的独有操作分别作了注释):
package List;
import Bag.LinearNode;
public class LinkedOrderedList implements ListADT {
private LinearNode front;
private int count;
public LinkedOrderedList()
{
front = null ;
count = 0 ;
}
// 三种链表的接口公共操作(实现是一样的)
public int size() {
return count;
}
public boolean isEmpty() {
return (count == 0 );
}
public Object first() {
if (size() == 0 ){
System.out.println( " 链表为空,没有元素 " );
return null ;
}
return front.getElement();
}
public Object last() {
if (size() == 0 ){
System.out.println( " 链表为空,没有元素 " );
return null ;
}
LinearNode last = front; // 遍历链表求last的代码
for ( int i = 0 ;i < count - 1 ;i ++ )
last = last.getNext();
return last.getElement();
}
public Object removeFirst() {
if (size() == 0 ){
System.out.println( " 链表为空,没有元素 " );
return null ;
}
Object result = front.getElement();
front = front.getNext();
count -- ;
return result;
}
public Object removeLast() {
if (size() == 0 ){
System.out.println( " 链表为空,没有元素 " );
return null ;
}
LinearNode last = front;
for ( int i = 0