数据结构-Java实现栈

概念

栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。栈是先进后出,出栈与入栈操作均在一端进行。

栈

本文使用数组实现栈、使用容器实现栈、使用链表实现栈、两个队列实现栈

数组实现栈

代码实现

package com.billJiang.stack;

import java.util.Arrays;
import java.util.EmptyStackException;

/**
 * Created by billJiang on 2016/11/29.
 * 用数组实现栈顶
 */
public class StackArray {

    private Object[] array;

    private int top;

    private int size=3;

    public StackArray(){
        array=new Object[size];
        top=-1;
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public Object  pop(){
        if(top==-1){
            throw new EmptyStackException();
        }
        return array[top--];
    }

    public Object peek(){
        if(top==-1)
            return null;
        return array[top];

    }

    public Object push(Object element){
        if(top==size-1){
           array= Arrays.copyOf(array,size*2);
        }
        array[++top]=element;
        return element;
    }


}

测试

package com.billJiang.stack;

/**
 * Created by billJiang on 2016/11/29.
 */
public class StackArrayTest {
    public static void main(String[] args) {
        StackArray stack=new StackArray();
        stack.push(new Integer(1));
        stack.push(new Integer(2));
        stack.push(new Integer(3));
        stack.push(new Integer(4));

        stack.peek();
        stack.pop();
        stack.push(new Integer(5));

    }
}

容器实现栈

代码实现

package com.billJiang.stack;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;

/**
 * Created by billJiang on 2016/11/30.
 * 用ArrayList容器实现栈
 */
public class StackList<T> {
    private List<T> list;
    private int size;

    public StackList(int size){
        this.size=size;
        list=new ArrayList<T>(size);
    }


    public T pop(){
        if(this.isEmpty()){
            throw new EmptyStackException();
        }
      return list.remove(list.size()-1);
    }

    public T peek(){
        if(this.isEmpty()){
            throw new EmptyStackException();
        }
        return list.get(list.size()-1);
    }

    public void push(T element){
        list.add(element);
    }

    public boolean isEmpty(){
       return list.isEmpty();
    }

    public int size(){
        return list.size();
    }

    public boolean isFull(){
        return list.size()==this.size;
    }
}

链表实现栈

链表节点定义

package com.billJiang.stack;

/**
 * Created by billJiang on 2016/11/30.
 * 单向链表节点
 */
public class Node<T> {
    private T item;

    private Node<T> next;

    public Node() {
        this.item = null;
        this.next = null;
    }

    public Node(T item, Node<T> next) {
        this.item = item;
        this.next = next;
    }

    public T getItem() {
        return item;
    }

    public void setItem(T item) {
        this.item = item;
    }

    public Node<T> getNext() {
        return next;
    }

    public void setNext(Node<T> next) {
        this.next = next;
    }
}

代码实现

package com.billJiang.stack;

import java.util.EmptyStackException;

/**
 * Created by billJiang on 2016/11/30.
 * 使用单项链表实现栈,出栈、入栈均在一头进行
 */
public class StackLinkedList<T> {

    private Node<T> top;

    public void push(T element) {
        if (isEmpty()) {
            top = new Node(element, null);
        }
        Node<T> node = new Node(element, top);
        top = node;
    }

    public T pop() {
        if (isEmpty())
            throw new EmptyStackException();
        T element = top.getItem();
        top = top.getNext();
        return element;
    }

    public T peek() {
        if (isEmpty())
            throw new EmptyStackException();
        return top.getItem();
    }

    public boolean isEmpty() {
        return top == null;
    }

}

两个队列实现栈

代码实现

思路:入栈在一个队列中进行,出栈时,将队列中其他元素移动另一个队列,剩下一个元素出队列,即出栈。

package com.billJiang.stack;

import com.billJiang.queue.QueueArray;

/**
 * Created by billJiang on 2016/12/1.
 * 两个队列实现栈
 */
public class StackQueue<T> {
    private QueueArray<T> queue1;
    private QueueArray<T> queue2;

    private int maxLength;

    public StackQueue(int initial) {
        this.maxLength = initial;
        queue1 = new QueueArray<>(initial);
        queue2 = new QueueArray<>(initial);

    }

    public boolean push(T element) {
        if (size() == maxLength) {
            return false;
        }
        if (queue2.isEmpty())
            queue1.put(element);
        else
            queue2.put(element);
        return true;
    }

    public T pop() {
        if(queue1.isEmpty()){
            while(queue2.size()>1){
                queue1.put(queue2.poll());
            }
            return queue2.poll();
        }else{
            while(queue1.size()>1){
                queue2.put(queue1.poll());
            }
            return queue1.poll();
        }

    }

    public T peek() {
        T obj=null;
        if(queue1.isEmpty()){
            while(queue2.size()>0){
                obj=queue2.poll();
                queue1.put(obj);
            }

        }else{
            while(queue1.size()>1){
                obj=queue1.poll();
                queue2.put(obj);
            }
        }
        return obj;
    }

    public int size() {
        return queue1.size() + queue2.size();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值