在这里插入图片描述

1、栈的基本概念

栈是一种抽象型数据结构,主要要两个特点:1.只能从栈顶端访问数据(获取和添加数据),2.数据的访问规则遵循 “先进后出” 的原则。如下图所示:
在这里插入图片描述

2、数组实现

package stack;
 
/**
 * 数组实现栈的操作
 */
public class StackDemo {
 
    private int maxSize; //栈的最大容量
    private int top;  //标记栈顶
    private int stack[] ;  //存储元素的数组
 
    public  StackDemo(int maxSize){
        this.maxSize=maxSize;
        stack=new int[maxSize];
        top=-1;
 
    }
 
    /**
     * 判断栈是否满
     * @return
     */
    public boolean isFull(){
        return top==maxSize-1;
    }
 
    /**
     * 判断栈是否为空
     * @return
     */
    public boolean isEmpty(){
        return top==-1;
    }
 
    /**
     * 向栈中添加元素
     */
    public void push(int value){
        if (!isFull()){
            top++;
            stack[top]=value;
        }
    }
 
    /**
     * 取出栈顶元素
     * @return
     */
    public int pop(){
        if (!isEmpty()){
            int value=stack[top];
            top--;
            return value ;
        }
        return -1;
 
    }
 
    /**
     * 遍历栈中的元素
     */
    public void list(){
        if (!isEmpty()){
            for (int i = top; i >=0 ; i--) {
                System.out.println(stack[i]);
 
            }
        }
    }
  
}

3、链表实现

2.2.1.节点代码

class StackNode{
    public Object object;
    public StackNode next;
    public StackNode(){
 
    }
    public StackNode(Object object,StackNode next){
        this.object=object;
        this.next=next;
    }
 
}

2.2.2.入栈出栈代码实现

/**
 * 利用链表实现栈
 */
public class LinkedStackDemo {
    private  StackNode first; //栈底端的指针
    private StackNode last; //栈顶端的指针
    private int size; //栈的大小
    /**
     * 栈是否为空
     * @return
     */
    public boolean isEmpty(){
        return first==null;
    }
 
    /**
     * 向栈中添加数据
     * @param data
     */
    public void push (Object data){
        StackNode newNode=new StackNode(data,null);
       if (isEmpty()){
           first=newNode;
 
       }else {
           last.next=newNode;
       }
        last=newNode;
        size++;
    }
 
    /**
     * 打印栈中元素
     */
    public void list() {
        StackNode current = first;
        StackNode before = null;
        while (current != null) {
            last = before;
            before = current;
            current = current.next;
            before.next = last;
        }
        current = before;
        while (current != null) {
            System.out.println(current.object);
            current = current.next;
        }
 
    }
 
    /**
     * 取出栈顶中的元素(其实就是删除链表尾部的数据)
     * @return
     */
    public Object pop(){
       if (!isEmpty()){
           StackNode current=first;
           while (current.next!=last){ //这样判断可以取出最后一个元素的之前的元素
               current=current.next;
           }
           StackNode lastNode =current.next; //最后一个节点
           current.next=null;
           last=current;
            return lastNode.object;
       }
       return null;
    }
 
 
}

每天进步一丢丢

完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值