Java与数据结构

使用java实现的一些数据结构
import java.util.ArrayList;
import java.util.List;

/**
    * Created by huangzhw on 2016/7/27.
    */
public class T {
                public static void main(String[] args) {
                                Heap<String> heap = new Heap<>(5);
                                heap.insertOne("A");
                                heap.insertOne("C");
                                heap.insertOne("D");
                                heap.insertOne("B");
                                heap.insertOne("E");
                                System.out.println(heap.sort());
                }
}
class Heap<T extends Comparable<? super T>>{
                private List<T> values;
                //堆的整体大小
                private final int maxSize;
                //最后的元素位置,初始为0(第一个元素不用了,任性);
                private int lastIndex;

                public Heap(int maxSize) {
                                this.maxSize = maxSize;
                                this.lastIndex = 0;
                                values = new ArrayList<>(this.maxSize+1);
                                values.add(null);
                }
                public boolean insertOne(T val){
                                if(isFull()) return false;
                                lastIndex++;
                                values.add(val);
                                moveUp(lastIndex);
                                return true;
                }
                public List<? super T> sort(){
                                List<? super T> result = new ArrayList<T>();
                                while(!isEmpty())
                                                result.add(popMax());
                                return result;
                }
                public T popMax(){
                                T val = values.get(1);
                                swapVal(1,lastIndex);
                                values.set(lastIndex,null);
                                lastIndex--;
                                moveDown(1);
                                return val;
                }
                public boolean isEmpty(){
                                return lastIndex <= 0;
                }
                public boolean isFull(){
                                return lastIndex == maxSize;
                }
                private void moveDown(int parent){
                                if(haveRightChild(parent)){
                                                int leftChild = getLeftChild(parent);
                                                int rightChild = getRightChild(parent);
                                                int candidateChild = values.get(leftChild).compareTo(values.get(rightChild)) >= 0?leftChild:rightChild;
                                                if(values.get(candidateChild).compareTo(values.get(parent)) > 0){
                                                                swapVal(parent,candidateChild);
                                                                moveDown(candidateChild);
                                                }
                                }else {
                                                if(haveLeftChild(parent)){
                                                                int leftChild = getLeftChild(parent);
                                                                if(values.get(parent).compareTo(values.get(leftChild)) < 0){
                                                                                swapVal(leftChild,parent);
                                                                }
                                                }
                                }
                }
                private void moveUp(int child){
                                int parent = getParent(child);
                                if(!isRoot(child) && values.get(parent).compareTo(values.get(child)) <= 0){
                                             swapVal(parent,child);
                                                moveUp(parent);
                                }
                }
                private void swapVal(int i1,int i2) {
                                T i1Val = values.get(i1);
                                values.set(i1,values.get(i2));
                                values.set(i2,i1Val);
                }
                private boolean haveLeftChild(int x){
                                return x*2 <= lastIndex;
                }
                private boolean isRoot(int x){
                                return x == 1;
                }
                private boolean haveRightChild(int y){
                                return y*2+1 <= lastIndex;
                }
                private int getLeftChild(int x){
                                return x*2;
                }
                private int getRightChild(int x){
                                return x*2+1;
                }
                private int getParent(int y){
                                return y / 2;
                }
}
  • 循环队列
import org.omg.CORBA.Object;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
    * Created by huangzhw on 2016/7/27.
    */
public class Main {
                public static void main(String[] args) {
                                CircleQueue<String> circleQueue = new CircleQueue<String>(3);
                                System.out.println(circleQueue.isEmpty());
                                circleQueue.addOne("1");
                                circleQueue.addOne("2");
                                circleQueue.addOne("3");
                                System.out.println(circleQueue.isFull());
                                System.out.println(circleQueue.removeOne());
                }
}
class CircleQueue<T>{
                private List<T> values;
                private int size;
                //尾元素的位置
                private int front;
                //头元素的位置
                private int rare;

                public CircleQueue(){
                                this(Integer.MAX_VALUE);
                }

                public CircleQueue(int size) {
                                this.size = size;
                                values = new ArrayList<T>(size);
                                front = size - 1;
                                rare = 0;
                }
                public boolean addOne(T val){
                                if(isFull()){
                                                return false;
                                }
                                front = addOne(front);
                                values.add(front,val);
                                return true;
                }
                public T removeOne(){
                                if(isEmpty())
                                                return null;
                                T val = values.get(rare);
                                values.set(rare,null);
                                rare = addOne(rare);
                                return val;
                }
                public boolean isFull(){
                                return addOne(addOne(front)) == rare;
                }
                public boolean isEmpty(){
                                return addOne(front) == rare;
                }
                private int addOne(int x){
                                return (x+1)%size;
                }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值