java 版 数据结构

sequence list

http://blog.csdn.net/baoyiming1991/article/details/6265743

public class SqList<E> {

	private static int INITSIZE=5;
	private static int INCREASESIZE = 2;
	
	private Object[] data = null;
	private int length;
	private int listsize;
	
	public SqList() {
		data = new Object[INITSIZE];
		length = 0;
		listsize = INITSIZE;
	}
	
	public boolean AddElement(E e) {
		if(length == listsize) {
			Object[] newdata = new Object[listsize+INCREASESIZE];
			for (int i=0;i<length;i++) {
				newdata[i] = data[i];
			}
			listsize = listsize+INCREASESIZE;
			data = newdata;
		}
		
		data[length] = e;
		length++;
		return true;
		
		
	}
	
	public E get(int index) {
		if (index <1 || index > length)
			return null;
		return (E) data[index-1];
	}
	
	public boolean set(int index, E e) {
		if(index <1 || index > length)
			return false;
		data[index-1] = e;
		return true;
	}
	
	public boolean insert(int index, E e) {
		if (index <1 || index >length+1)
			return false;
		if (e==null)
			return false;
		if (length == listsize) {
			
		}
		for (int i=length-1; i>=index-1;i--) {
			data[i+1] = data[i];
		}
		data[index-1] = e;
		return true;
	}
	
	public boolean delete(int index) {
		
		if(index<1 || index > this.length) {
			return false;
		}
		for (int i=index; i<this.length; i++) {
			data[i-1] = data[i];
		}
		this.length--;
		return true;
		
	}

}

Link List   http://blog.csdn.net/baoyiming1991/article/details/6266147

public class LinkList<E> {

	public LLNode<E> head;
	
	public LinkList() {
		head = new LLNode<E>();
		head.next = null;
	}
	
	public boolean add(E e) {
		LLNode<E> cursor = head;
		while (cursor.next!=null) {
			cursor = cursor.next;
		}
		LLNode<E> newnode = new LLNode<E>();
		newnode.data = e;
		newnode.next = null;
		cursor.next = newnode;
		return true;
	}
	
	public boolean insert(int index, E e) {
		if (e==null)
			return false;
		LLNode<E> cursor = head;
		int count = 1;
		while (count < index && cursor!=null) {
			count++;
			cursor = cursor.next;
		}
		if(count == index && cursor !=null) {
			LLNode<E> newnode = new LLNode<E>();
			newnode.data = e;
			LLNode<E> temp = cursor.next;
			cursor.next = newnode;
			newnode.next = temp;
			return true;
		}
		return false;
		
	}
	
	public E get(int index) {
		if(index<1)
			return null;
		LLNode<E> cursor = head.next;
		int count = 1;
		while (count < index && cursor !=null) {
			count++;
			cursor=cursor.next;
		}
		if (count == index && cursor !=null) {
			return cursor.data;
		}
		return null;		
	}
	
	public boolean delete(int index) {
		if(index<1)
			return false;
		
		int count =1;
		LLNode<E> cursor = head;
		
		while (count<index && cursor.next !=null) {
			count ++;
			cursor = cursor.next;
		}
		if (count == index && cursor.next !=null) {
			
			return true;
		}
		return false;
	}

}

class LLNode<E> {
	public E data;
	public LLNode<E> next;
}

stack and queue     http://blog.csdn.net/baoyiming1991/article/details/6266339

相关推荐