java数据结构:堆栈

堆栈的数据结构只允许数据从有序列表的一端做输入输出操作。

堆栈数据结构的特点是先进入的数据后处理,后进入的数据先处理,就比如子弹入弹匣一下,先压入弹匣的子弹后面发射出,后压入的子弹先发射。

下面是用数组模拟堆栈:ArrayOfStack.java

package Stack;

/**
 * @author zh
 * 数组实现堆栈
 */
public class ArrayOfStack {

    private Object[] stack = null;
    private int size;
    private int top;

    //默认初始化一个栈堆,大小为100
    public ArrayOfStack(){
        this.size = 100;
        this.top = -1;
        this.stack = new Object[size];
    }

    //初始化一个自定义大小的栈堆
    public ArrayOfStack(int size){
        this.size = size;
        this.top = -1;
        this.stack = new Object[size];
    }

    //判断堆栈是否为空
    public boolean isEmpty(){
        if(top == -1){
            return true;
        }else
            return false;
    }

    //判断栈堆是否已满
    public boolean isFull(){
        if(top == (size-1)){
            return true;
        }else
            return true;
    }

    //入栈操作
    public void push(String data){
        if(isFull()){
            System.out.println("堆栈已满");
            return;
        }
        //入栈开始,top相当于数组下标
        top++;
        stack[top] = data;
    }

    //出栈
    public Object pop(){
        //定义一个临时对象
        Object val = null;
        //堆栈为空时报错
        if(isEmpty()){
            System.out.println("堆栈为空");
            return val;
        }
        //获取堆栈值
        val = stack[top];
        top--;
        return val;
    }

    //获取栈顶元素
    public Object peek(){
        Object topVal = null;
        if(isEmpty()){
            System.out.println("栈堆为空!");
            return topVal;
        }
        //取出栈顶元素
        topVal = stack[top];
        return topVal;
    }

    //获取堆栈元素
    public int size(){
        return top+1;
    }

    //获取栈堆容量
    public int capcity(){
        return size;
    }
}

使用链表实现堆栈:

节点类Node.java

package Stack;

public class Node {

    //链表数据域
    Object data;
    //链域
    Node next;

    //构造头结点
    public Node(){
        this.data = null;
        this.next = null;
    }
    //构造节点
    public Node(Object data){
        this.data = data;
        this.next = null;
    }
}

堆栈类:LinkOfStack.java

package Stack;

public class LinkOfStack {
    //定义一个头结点
    private Node head;
    //构造头结点
    public LinkOfStack(){
        head = new Node();
    }

    //判断是否为空
    public boolean empty(){
        if(head.next == null)
            return true;
        else
            return false;
    }

    //堆栈入栈
    public void push(Object data){
        Node item = new Node(data);
        item.next = head.next;
        head.next = item;
    }

    //出栈
    public Object pop(){
        Object item = null;
        if(empty()){
            System.out.println("堆栈为空");
        }
        item = head.next.data;
        head.next = head.next.next;
        return item;
    }

    //堆栈大小
    public int size(){
       int len = 0;
       Node p = head;
       while(p.next != null){
           len++;
           p = p.next;
       }
       return len;
    }

    //获取栈顶元素
    public Object peek(){
        if(empty()){
            System.out.println("堆栈为空");
        }
        return head.next.data;
    }

    public static void main(String[] args){
        LinkOfStack stack = new LinkOfStack();
        stack.push("测试链表堆栈节点一");
        System.out.println(stack.size());
        System.out.println(stack.peek());
        while(!stack.empty()){
            System.out.print(stack.pop()+"-->");
        }
    }
}