有序链表
有序链表:插入的元素呈有序状态,删除链头时,就删除最小(或最大)的值,插入时,需要从链头开始搜寻插入的位置。
插入的时间复杂度O(n),平均O(n/2),删除最小(或最大值)的时间复杂度O(1)。
适用场所:适用于需要存取(插入/查找/删除)最小(或最大)数据项的应用。
有序链表接口
public interface SortedListInterface <T extends Comparable<? super T>>{
public boolean add(T newEntry);
public boolean remove1(T anEntry);
public int getPosition(T anEntry);
public T getEntry(int givenPosition);
public boolean contains(T anEntry);
public T remove2 (int givenPosition);
public void clear();
public int getLength();
public boolean isEmpty();
public boolean isFull();
public void display();
public Iterator<T> getIterator();
}
有序链表实现类
public class SortedLinkedList <T extends Comparable<? super T>> implements SortedListInterface<T>{
private Node firstNode;
private int length;
public SortedLinkedList() {
firstNode=null;
length=0;
}
private Node getNodebefore(T anEntry){
Node currentNode=firstNode;
Node nodeBefore=null;
while(currentNode!=null && anEntry.compareTo(currentNode.getData())>0){
nodeBefore=currentNode;
currentNode=currentNode.getNextNode();
}
return nodeBefore;
}
/**
* 迭代方式
*/
@Override
public boolean add(T newEntry) {
Node newNode=new Node(newEntry);
Node nodeBefore=getNodebefore(newEntry);
if(isEmpty()||nodeBefore==null){
newNode.setNextNode(firstNode);
firstNode=newNode;
}else{
Node nodeAfter=nodeBefore.getNextNode();
newNode.setNextNode(nodeAfter);
nodeBefore.setNextNode(newNode);
}
length++;
return true;
}
/**
* 递归方式
* @param newEntry
* @return
*/
/* public boolean add(T newEntry) {
firstNode=add(newEntry, firstNode);
length++;
return true;
}
private Node add(T newEntry,Node currentNode){
if(currentNode==null||newEntry.compareTo(currentNode.getData())<=0)
currentNode=new Node(newEntry,currentNode);
else{
Node nodeAfter=add(newEntry,currentNode.getNextNode());
currentNode.setNextNode(nodeAfter);
}
return currentNode;
}*/
@Override
public boolean remove1(T anEntry) {
boolean result=false;
Node currentNode=firstNode;
Node nodeBefore=null;
while(currentNode!=null && anEntry.compareTo(currentNode.getData())>=0){
if(anEntry.compareTo(firstNode.getData())==0){
firstNode=firstNode.getNextNode();
currentNode=firstNode;
nodeBefore=null;
result=true;
}else if(anEntry.compareTo(currentNode.getData())==0){
currentNode=currentNode.getNextNode();
nodeBefore.setNextNode(currentNode);
result=true;
}else{
nodeBefore=currentNode;
currentNode=currentNode.getNextNode();
}
}
return result;
}
@Override
public int getPosition(T anEntry) {
Node currentNode=firstNode;
int position=0;
while(currentNode!=null && anEntry.compareTo(currentNode.getData())>0){
currentNode=currentNode.getNextNode();
position++;
}
if(anEntry.compareTo(currentNode.getData())==0)
position++;
else
position=0;
return position;
}
@Override
public T getEntry(int givenPosition) {
Node currentNode=firstNode;
for(int position=1;position<givenPosition;position++){
currentNode=currentNode.getNextNode();
}
return currentNode.getData();
}
@Override
public boolean contains(T anEntry) {
boolean result=false;
Node currentNode=firstNode;
while(currentNode!=null){
if(anEntry.compareTo(currentNode.getData())==0){
result=true;
break;
}
currentNode=currentNode.getNextNode();
}
return result;
}
@Override
public T remove2(int givenPosition) {
Node currentNode=firstNode;
Node nodebofore=null;
int position=1;
if(givenPosition<=length){
while(position<givenPosition){
nodebofore=currentNode;
currentNode=currentNode.getNextNode();
position++;
}
nodebofore.setNextNode(currentNode.getNextNode());
}
return currentNode.getData();
}
@Override
public void clear() {
firstNode=null;
length=0;
}
@Override
public int getLength() {
return length;
}
@Override
public boolean isEmpty() {
return length==0;
}
@Override
public boolean isFull() {
return false;
}
@Override
public void display() {
Iterator iterator=getIterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
}
@Override
public Iterator<T> getIterator() {
return new IteratorForLinkedList();
}
/**
* 内部类迭代器
* @author Administrator
*
*/
private class IteratorForLinkedList implements Iterator<T>{
private Node nextNode;
private IteratorForLinkedList() {
nextNode=firstNode;
}
@Override
public boolean hasNext() {
return nextNode!=null;
}
@Override
public T next() {
if(hasNext()){
Node returnNode=nextNode;
nextNode=nextNode.getNextNode();
return returnNode.element;
}else
throw new NoSuchElementException("Illegal call to next();"+"iteration is after end of list.");
}
public void remove(){//不允许在历遍过程中删除
throw new UnsupportedOperationException("remove() is not suported.");
}
}
private class Node {
private T element;
private Node next;
private Node(T element) {
this(element,null);
}
private Node(T element,Node next){
this.element=element;
this.next=next;
}
private T getData(){
return element;
}
private void setData(T newData){
element=newData;
}
private Node getNextNode(){
return next;
}
private void setNextNode(Node nextNode){
next=nextNode;
}
}
}
测试程序
public class main_sortedlist {
private static SortedLinkedList<Integer> list;
public static void main(String[] args) {
Random ra =new Random();
list=new SortedLinkedList<>();
for(int i=0;i<30;i++)
list.add(ra.nextInt(100)+1);
list.display();
list.remove1(30);
list.display();
System.out.println(list.contains(list.getEntry(12)));
list.remove2(15);
list.display();
}
}