`

数据结构-----基本结构

阅读更多

public class MyArray {
	private int[] array;
	private int lastIndex = -1;
	private int length;
	
	public MyArray(){
		array = new int[50];
		this.length = 50;
	}
	
	public MyArray(int length){
		array = new int[length];
		this.length = length;
		lastIndex = length-1;
	}
	
	/*
	 * 按索引顺序加入
	 */
	public void add(int value){
		if(isEnded())throw new ArrayIndexOutOfBoundsException();
		lastIndex++;
		array[lastIndex] = value;
		
	}
	
	/**
	 * 有序插入,从小到大
	 * @param value
	 */
	public void add2(int value){
		int i ;
		if(isEnded())throw new ArrayIndexOutOfBoundsException();
		for(i=0;i<=lastIndex;i++){
			if(value<array[i]){
				for(int j=lastIndex;j>=i ;j--){
					array[j+1]=array[j];
				}
				array[i] = value;
				break;
			}
		}
		if(i>lastIndex)array[i]=value;
		lastIndex ++;
		
		
	}
	
	public int valueOf(int index){
		if(isEnded(index)) return -1;
		
		return array[index];
	}
	public void remove(int  index){
		if(isOutOfBoundary(index)) throw new ArrayIndexOutOfBoundsException();
		else if(!isEnded(index)){
			for(int i=index; i<=lastIndex;i++){
				array[i] = array[i+1];
			}
		}
		lastIndex --;
	}
	
	/**
	 * 线性查找
	 * @param value
	 * @return
	 */
	public long indexOf(int value){
		for(int i=0;i<=lastIndex;i++){
			if(value == array[i])return i;
		}
		return -1;
	}
	
	/*
	 * 二分法查找:前提:有序数组
	 * 
	 */
	public int binarySearch(int value){
		int low = 0;
		int hight = lastIndex;
		int middle ;
		while(true){
			middle = (low + hight)/2;
			if(low>hight)return -1;
			else if(value>array[middle] ){
				low = middle+1;
				
			}else if(value<array[middle]){
				hight = middle -1;
			}else if(value == array[middle] )return middle;
		}
	}
	
	/**
	 * 冒泡排序
	 */
	public void bubbleSort(int value){
		
		for(int i=0;i<lastIndex;i++){//i表示趟数,因为每次上升一个泡泡
			//j是扫描指针,扫一个就交换一次,所以他正好也记录的当前最小的
			for(int j=lastIndex;j>i;j--){ 
				
				bubble(array[j],array[j-1]);
				
			}
		}
	}
	
	
	private void bubble(int bubble1, int bubble2) {
		if(bubble1 < bubble2){
			int temp = bubble1;
			bubble1 = bubble2;
			bubble1 = temp;
		}
		
	}

	public String toString(){
		String arrStr = "[";
		for(int i=0;i<lastIndex;i++){
			arrStr += array[i]+",";
		}
		
		arrStr +=array[lastIndex]+"]";
		return arrStr;
	}
	
	public boolean isEnded(){
		if(length == lastIndex + 1)return true;
		return false;
	}
	public boolean isEnded(int index){
		if(length == index + 1)return true;
		return false;
	}
	public boolean isOutOfBoundary(int index){
		if(index <0 || index >lastIndex)return true;
		else return false;
	}
}



LinkedList

package com.zhe.arr;

public class LinkedList {
	Node first; //头结点,里面的data域是空值
	
	public LinkedList(){
		first = new Node();
	}
	
	public void add2First(int value){
		Node node = new Node(value);
		node.next = first.next;
		first.next = node;
	}
	
	public Node removeFromFirst(){
		Node node = first.next;
		first.next = node.next;
		return node;
	}
	
	public Node find(int data){
		Node current = first.next;
		while(current != null ){
			if(current.data == data)break;
			current = current.next;
		}
		return current;
	}
	
	public void display(){
		Node current = first.next;
		while(current != null){
			System.out.print(current);
			current = current.next;
		}
	}
}


Info
package com.zhe.arr;
/**
 * 员工信息类
 * @author Administrator
 *
 */
public class Info {
	private String key;
	private String value;
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}

package com.zhe.arr;

public class Node {
	int data;
	Node next;
	
	public Node(){
		
	}
	
	public Node(int data){
		this.data = data;
	}
	
	public String toString(){
		return data + " ";
	}
}


package com.zhe.arr;

import java.math.BigInteger;

/**
 * hashTable就是根据对象的某个值算出在数组中的位置,从而快速定位对象所在的地方
 * @author Administrator
 *
 */
public class HashTable {
	private Info[] arr;
	private int elements;
	public HashTable(){
		arr = new Info[100];
		elements = 0;
	}
	
	public HashTable(int length){
		arr = new Info[length];
		elements = 0;
	}

	public void insert(Info info){
		if(elements == arr.length)return;
		if(arr[hashCode(info.getKey())] != null ){
			int i=0;
			while(arr[hashCode(info.getKey())+i]!= null){
				if(i+1 == arr.length)i=0;
				else i++;
			}
			arr[hashCode(info.getKey())+i] = info;
			elements ++;
		}
	}
	
	public int hashCode(String key) {
		//方法   1*27 + 2*27 + 。。。。
		BigInteger hasVal = new BigInteger("0");
		for(int i = 0 ;i<key.length();i++){
			hasVal = new BigInteger(String.valueOf(key.charAt(i) - 96))
			            .multiply(new BigInteger("27"))
			            .multiply(new BigInteger(String.valueOf(i+1)));
		}
		return hasVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
	}
	
	
}


package com.zhe.arr;
/**
 * 这里的队列使用循环队列
 * @author Administrator
 *
 */
public class MyQueue {
	
	private long[] arr;
	int first = -1; //指向第一个元素的前一个空
	int end = -1;  //由初始化可知 first == end 成立时是空队列
	
	public MyQueue(){
		arr = new long[10];
	}
	
	public MyQueue(int length){
		arr = new long[length];
	}
	
	public void push(long value){
		end++;
		if(end == arr.length)end = 0;
		arr[end] = value;
	}
	
	public Long remove(){
		if(isEmpty())return null;
		first ++;
		if(first == arr.length) first = 0;
		return arr[first];
	}

	private boolean isEmpty() {
		return end == first;
	}
	
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics