使用数组和链表实现线性表

package edu.cumt.jnotnull;

public interface ListInterface<T> {
	public boolean add(T newEntry);
	
	public boolean add(int newPosition,T newEntry);
	
	public T remove(int givenPosition);
	
	public void clear();
	
	public boolean replace(int givenPosition,T newEntry);
	
	public T getEntry(int givenPosition);
	
	public boolean contains(T anEntry);
	
	public int getLength();
	
	public boolean isEmpty();
	
	public boolean isFull();
	
	public void display();
}

 

 上面代码是公共接口,下面是数组实现的线性表

package edu.cumt.jnotnull;

public class AList<T> implements ListInterface<T> {
	private T[] entry;
	private int length;
	private static final int MAX_SIZE = 50;
	
	public AList(){
		this(MAX_SIZE);
	}
	
	@SuppressWarnings("unchecked")
	public AList(int maxSize){
		length = 0;
		entry = (T[])new Object[maxSize];
	}

	@Override
	public boolean add(T newEntry) {
		boolean isSuccessful = true;
		if(!isFull()){
			entry[length] = newEntry;
			length++;
		}else
			isSuccessful = false;
		return isSuccessful;
	}

	@Override
	public boolean add(int newPosition, T newEntry) {
		boolean isSuccessful = true;
		if(!isFull()&&(newPosition >=1)&&(newPosition<=length+1)){
			makeRoom(newPosition);
			entry[newPosition-1] = newEntry;
			length++;
		}else
			isSuccessful = false;
		return isSuccessful;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean contains(T anEntry) {
		boolean found = false;
		for(int index = 0;!found&&(index<length);index++){
			if(anEntry.equals(entry[index]))
				found = true;
		}
		return found;
	}

	@Override
	public void display() {
		for(int index = 0;index<length;index++){
			System.out.println(entry[index]);
		}
	}

	@Override
	public T getEntry(int givenPosition) {
		T result = null;
		if((givenPosition >= 1)&&(givenPosition <= length)){
			assert !isEmpty();
			result = entry[givenPosition - 1];
		}
		return result;
	}

	@Override
	public int getLength() {
		return length;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return length == 0;
	}

	@Override
	public boolean isFull() {
		// TODO Auto-generated method stub
		return length == entry.length;
	}

	@Override
	public T remove(int givenPosition) {
		T result = null;
		
		if((givenPosition >= 1)&&(givenPosition <= length)){
			assert !isEmpty();
			result = entry[givenPosition -1];
			if(givenPosition < length){
				removeGap(givenPosition);
			}
			length--;
		}
		return result;
	}

	@Override
	public boolean replace(int givenPosition, T newEntry) {
		boolean isSuccessful = true;
		if((givenPosition >= 1)&&(givenPosition <= length)){
			entry[givenPosition-1] = newEntry;
		}
		else
			isSuccessful = false;
		return isSuccessful;
	}

	
	private void makeRoom(int newPosition){
		for(int index = length;index>=newPosition;index--){
			entry[index] = entry[index-1];			
		}
	}
	
	private void removeGap(int givenPosition){
		assert(givenPosition >=1)&&(givenPosition < length);
		int removedIndex = givenPosition -1;
		int lastIndex = length - 1;
		for(int index = removedIndex;index<lastIndex;index++){
			entry[index] = entry[index+1];			
		}
	}
	

}

 

 链表实现的线性表,在这里可以加入表尾引用lastNode,进一步的提高效率.

package edu.cumt.jnotnull;

public class LList<T> implements ListInterface<T> {

	private Node<T> firstNode;
	//private Node<T> lastNode;
	private int length;
	
	public LList(){
		clear();
	}
	
	@Override
	public boolean add(T newEntry) {		
		Node<T> newNode = new Node<T>(newEntry);
		if(isEmpty()){
			firstNode = newNode;
		}
		else{
			Node<T> lastNode = getNodeAt(length);
			lastNode.next = newNode;
		}
		length ++;
		return true;
	}

	@Override
	public boolean add(int newPosition, T newEntry) {
		if((newPosition >= 1)&&(newPosition <= length-1)){
			Node <T>newNode = new Node<T>(newEntry);
			if(isEmpty()||(newPosition == 1)){
				newNode.next = firstNode;
				firstNode = newNode;
			}else{
				Node<T> nodeBefore = getNodeAt(newPosition - 1);
				Node<T> nodeAfter = nodeBefore.next;
				newNode.next = nodeAfter;
				nodeBefore.next = newNode;
			}
			length ++;
		}
		return true;
	}

	@Override
	public final void clear() {
		firstNode = null;
		//lastNode = null;
		length = 0;
	}

	@Override
	public boolean contains(T anEntry) {
		boolean found = false;
		Node <T>currentNode = firstNode;
		while(!found&&(currentNode!=null)){
			if(anEntry.equals(currentNode.data)){
				found = true;
			}else
				currentNode = currentNode.next;
		}
		return found;
	}

	@Override
	public void display() {
		Node<T> currentNode = firstNode;
		while(currentNode != null){
			System.out.println(currentNode.data);
			currentNode = currentNode.next;
		}
	}

	@Override
	public T getEntry(int givenPosition) {
		T result = null;
		if(!isEmpty()&&(givenPosition>=1)&&(givenPosition<=length)){
			result = getNodeAt(givenPosition).data;
		}
		return result;
	}

	@Override
	public int getLength() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isEmpty() {
		boolean result;
		if(length == 0){
			assert firstNode == null;
			result = true;
		}
		else{
			assert firstNode != null;
			result = false;
		}
		return result;
	}

	@Override
	public boolean isFull() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public T remove(int givenPosition) {
		T result = null;
		if(!isEmpty()&&(givenPosition>=1)&&(givenPosition<=length)){
			if(givenPosition == 1){
				result = firstNode.data;
				firstNode = firstNode.next;
			}
			else{
				Node<T> nodeBefore = getNodeAt(givenPosition - 1);
				Node<T> nodeToRemove = nodeBefore.next;
				Node<T> nodeAfter = nodeToRemove.next;
				nodeBefore.next = nodeAfter;
				result = nodeToRemove.data;
			}
		}
		return result;
	}

	@Override
	public boolean replace(int givenPosition, T newEntry) {
		boolean isSuccessful = true;
		if(!isEmpty()&&(givenPosition>=1)&&(givenPosition<=length)){
			Node <T>desiredNode = getNodeAt(givenPosition);
			desiredNode.data = newEntry;
		}else
			isSuccessful = false;
		return isSuccessful;
	}
	
	private Node<T> getNodeAt(int givenPosition){
		assert !isEmpty()&&(1<=givenPosition)&&(givenPosition <=length);
		Node<T> currentNode = firstNode;
		for(int counter = 1;counter<givenPosition;counter++){
			currentNode = currentNode.next;			
		}
		assert currentNode != null;
		return currentNode;
	}

	
	private class Node<T> {
		private T data;
		private Node<T> next;
		
		private Node(T dataPortion){
			data = dataPortion;
			next = null;
		}
		
		private Node(T dataPortion,Node<T> nextNode){
			data = dataPortion;
			next = nextNode;
		}
	}
	
}

 

你可能感兴趣的:(java数据结构)