昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

基于动态数组技术实现的栈

栈接口

package Stack;
interface Stack<ElementType> {
    void push(ElementType elementType);
    ElementType pop();
    ElementType peek();
    int getSize();
    boolean isEmpty();
}


Array

package Stack;
public class Array<ElementType> {//泛型需要注意的地方
    private ElementType[] data;
    private int size;
    public Array(int capacity) {//构造函数,传入数组的容量capacity构造Array
        data=(ElementType[])new Object[capacity];//泛型需要注意的地方
        size=0;
    }
    public Array() {//无参构造函数,默认数组的容量capacity=10
        this(10);
    }
    public int getSize() {//获取数组中的元素个数
        return size;
    }

    public int getCapacity() {//获取数组的容量
        return data.length;
    }
    public boolean isEmpty(){//返回数组是否为空
        return size==0;
    }
    public void addLast(ElementType e){//向所有元素后添加一个新的元素
        add(size,e);
    }
    public void addFirst(ElementType e){//在所有元素前面添加一个新的元素
        add(0,e);
    }
    public void add(int index,ElementType e){//在第index个位置插入一个新的元素e
        if(index<0||index>size){
            throw new IllegalArgumentException("Add failed.required index>=0 and  index<=size.");
        }
        if(size==data.length){
            resize(2*data.length);
        }
        for (int i =size-1 ; i >=index ; i--) {
            data[i+1]=data[i];
        }
        data[index]=e;
        size++;
    }
    public ElementType get(int index){ //获取index索引位置的元素
        if (index<0||index>=size){
            throw new IllegalArgumentException("Set failed,Index is illegal.");
        }
        return data[index];
    }

    public ElementType getLast(){//获取最后一个元素
        return get(size-1);
    }
    public ElementType getFirst(){//获取第一个元素
        return get(0);
    }
    public void set(int index,ElementType e){//修改index 索引位置的元素为e
        data[index]=e;
    }
    public boolean contains(ElementType e){//查找数组中的元素是否有元素e
        for (int i = 0; i <size ; i++) {
            if (data[i].equals(e)/*泛型注意*/){
                return true;
            }
        }
        return false;
    }
    public int find(int e){ //查找数组中元素e所在的索引,如果不存在元素e,则返回-1
        for (int i = 0; i <size ; i++) {
            if (data[i].equals(e)/*泛型注意*/){
                return i;
            }
        }
        return -1;
    }
    //从数组中删除index位置的元素,返回删除的元素
    public ElementType remove(int index){
        if (index<0||index>=size){
            throw new IllegalArgumentException("Remove failed,Index is illegal.");
        }
        ElementType result=data[index];
        for (int i = index+1; i <size ; i++) {
            data[i-1]=data[i];
        }
        size--;
        data[size]=null;//泛型注意
        if (size==data.length/2){//缩容技术处理
            resize(data.length/2);
        }
        return result;
    }
    public ElementType removeFirst(){//从数组中删除第一个元素
        return remove(0);
    }
    public ElementType removeLast(){ //从数组中删除最后一个元素
        return remove(size-1);
    }
    public void removeElement(int e){//从数组中删除元素e
        int index=find(e);
        if (index!=-1){
            remove(index);
        }
    }
        @Override
    public String toString(){//打印数组元素
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(String.format("Array:size=%d,capacity=%d\n",size,data.length));
        stringBuffer.append('[');
        for (int i = 0; i <size ; i++) {
            stringBuffer.append(data[i]);
            if (i!=size-1){
                stringBuffer.append(",\t");
            }
        }
        stringBuffer.append("]");
        return stringBuffer.toString();
    }
    // 将数组空间的容量变成newCapacity大小
    private void resize(int newCapacity){//扩容技术处理

        ElementType[] newData = (ElementType[])new Object[newCapacity];
        for(int i = 0 ; i < size ; i ++)
            newData[i] = data[i];
        data = newData;
    }
}

ArrayStack

package Stack;

import java.lang.annotation.ElementType;

public class ArrayStack<ElementType> implements Stack<ElementType>{
    private Array<ElementType> array;

    public ArrayStack(int capacity) {
        array=new Array<ElementType>(capacity);
    }

    public ArrayStack() {
        array=new Array<ElementType>();
    }

    @Override
    public void push(ElementType elementType) {
        array.addLast(elementType);
    }

    @Override
    public ElementType pop() {
        return array.removeLast();
    }

    @Override
    public ElementType peek() {
        return array.getLast();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }
    public int getCapacity(){
        return array.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder result=new StringBuilder();
        result.append("Stack:\t");
        result.append('[');
        for (int i = 0; i <array.getSize() ; i++) {
            result.append(array.get(i));
            if(i!=array.getSize()-1){
                result.append(",\t");
            }
        }
        result.append("]\ttop");
        return result.toString();
        /*return "ArrayStack{" +
                "array=" + array +
                '}';*/
    }
}


Driver

package Stack;

import java.lang.annotation.ElementType;

public class Driver {
    public static void main(String[] args) {
        ArrayStack<Integer> arrayStack=new ArrayStack<Integer>();
        for (int i = 0; i <5 ; i++) {
            arrayStack.push(i);
            System.out.println(arrayStack);
        }
        arrayStack.pop();
        System.out.println(arrayStack);
    }
}

posted on 2018-05-26 17:23  Indian_Mysore  阅读(72)  评论(0编辑  收藏  举报

导航