Java棧實現

1. 定義棧全部方法接口:
package com.dataStructure.stack;

/**
* 類功能描述:
*
* @version 1.0.0
* @auther Create by Barry
* @date Create on 2018/3/12.
* @history
*/
public interface Stack {
public boolean push(Object element);

public Object pop();

public Object peek();

public int elementCount();

public boolean isEmpty();

public void showElements();
}

2. 基於數組的棧實現

package com.dataStructure.stack;

/**
* 類功能描述:
*
* @version 1.0.0
* @auther Create by Barry
* @date Create on 2018/3/12.
* @history
*/
public class ArrayStack implements Stack{
private int maxSize;
private Object data[];
private int top = -1;

public ArrayStack(int maxSize){
data = new Object[maxSize];
this.maxSize = maxSize;
}

@Override
public boolean push(Object element){
if(top == maxSize-1){
return false;
}
data[++top] = element;
return true;
}

@Override
public Object pop(){
if(this.isEmpty()){
return -1;
}
Object returnData = data[top];
top--;
return returnData;
}

@Override
public Object peek(){
if(this.isEmpty()){
return -1;
}
return data[top];
}

@Override
public int elementCount(){
return top+1;
}

@Override
public boolean isEmpty(){
if(top == -1){
return true;
}
return false;
}

@Override
public void showElements(){
System.out.println("--------Show all element start--------");
for(int i=top; i>=0; i--){
System.out.println(data[i]);
}
System.out.println("--------Show all element end--------");
}

public static void main(String[] args){
Stack stack = new ArrayStack(11);

/*System.out.println("=============================test 1 start--------------");
stack.push(1);
stack.showElements();
System.out.println(stack.elementCount());
System.out.println("=============================test 1 end--------------");
System.out.println("=============================test 2 start--------------");
stack.push(2);
stack.showElements();
System.out.println(stack.elementCount());
System.out.println("=============================test 2 end--------------");
System.out.println("=============================test 3 start--------------");
stack.pop();
stack.showElements();
System.out.println(stack.elementCount());
System.out.println("=============================test 3 end--------------");*/
/*System.out.println("=============================test 4 start--------------");
for(int i=1; i<=12; i++ ){
stack.push(i);
}
stack.showElements();
System.out.println(stack.elementCount());
System.out.println("=============================test 4 end--------------");*/

String str = "1234321";
System.out.println(ArrayStack.isSysmetryStr(str));
}

/**
* 用棧判斷字符串是否爲對稱串,
* 即從左到右和從右到左讀取是同樣的
* 1234321爲對稱串
* @param str
* @return
*/
public static boolean isSysmetryStr(String str){
if(str == null || str.length() == 0){
return true;
}

Stack stack = new ArrayStack(str.length());
for(int i=0; i < str.length(); i++){
stack.push((int)str.charAt(i));
}

for(int i=0; i < str.length(); i++){
Object data = stack.pop();
if((int)data == (int)str.charAt(i)){
return false;
}
}

return true;
}
}

3. 基於鏈表的棧實現
package com.dataStructure.stack;/** * 類功能描述: * * @version 1.0.0 * @auther Create by Barry * @date Create on 2018/3/12. * @history */public class LinkedStack implements Stack{    private Node header;    private int elementCount = 0;    private int maxSize;    public LinkedStack(int maxSize){        this.maxSize = maxSize;    }    @Override    public boolean push(Object data) {        if(elementCount == maxSize){            throw new RuntimeException("Stack is Full");        }        header = new Node(data, header);        elementCount++;        return true;    }    @Override    public Object pop() {        if(isEmpty()){            throw new RuntimeException("Stack is empty");        }        Object returnData = header.data;        header = header.next;        elementCount--;        return returnData;    }    @Override    public Object peek() {        return header.data;    }    @Override    public int elementCount() {        return elementCount;    }    @Override    public boolean isEmpty() {        return (elementCount == 0);    }    @Override    public void showElements() {        while (header != null){            System.out.println(header.data);            header = header.next;        }    }    class Node{        private Object data;        private Node next;        public Node(Object data){            this(data, null);        }        public Node(Object data, Node next){            this.data = data;            this.next = next;        }    }    public static void main(String[] args) {        Stack stack = new ArrayStack(5);        for(int i=1; i<=5; i++){            stack.push(i);        }        stack.pop();        stack.showElements();    }}
相關文章
相關標籤/搜索