0
点赞
收藏
分享

微信扫一扫

Java手写实现栈【数据结构与算法】

q松_松q 2022-10-03 阅读 235

package algorithm;

import java.util.Arrays;
import java.util.Iterator;

/**

  • @author Administrator
  • @date 2022-09-12 16:38
  • 数组栈
    */
    public class MyArrayStack {
    // 定义一个数组
    private Object[] elementData;
    // 顶部的索引
    private int topIndex;
    // 构造方法确定栈的长度
    public MyArrayStack(int size) {
    this.elementData = new Object[size];
    }
    /**
  • 定义迭代器
  • @return
    */
    @Override
    public Iterator iterator() {
    return new MyArrayStackIter();
    }
    class MyArrayStackIter implements Iterator{
    @Override
    public boolean hasNext() {
    return topIndex != elementData.length;
    }
    @Override
    public E next() {
    return pop();
    }
    @Override
    public void remove() {
    pop();
    }
    }
    public boolean push(E element){
    // 判断扩容两倍
    if(topIndex >= elementData.length){
    elementData = Arrays.copyOf(elementData,elementData.length << 1);
    }
    elementData[topIndex++] = element;
    return true;
    }
    // 删除并出栈 是否删除就看topIndex本身的大小是否改变
    public E pop(){
    if(topIndex <= 0){
    throw new RuntimeException("栈为空");
    }
    return (E) elementData[--topIndex];
    }
    // 不删除出栈 栈顶元素
    public E peek(){
    if(topIndex <= 0){
    throw new RuntimeException("栈为空");
    }
    return (E) elementData[topIndex - 1];
    }
    public static void main(String[] args) {
    MyArrayStack myArrayStack = new MyArrayStack(5);
    myArrayStack.push("11");
    myArrayStack.push("22");
    myArrayStack.push("33");
    System.out.println("-----------");
    System.out.println(myArrayStack.pop());
    System.out.println(myArrayStack.pop());
    System.out.println(myArrayStack.pop());
    System.out.println(myArrayStack.pop());
    // Iterator iterator = myArrayStack.iterator();
    // while (iterator.hasNext()){
    // System.out.println(iterator.next());
    // }
    }
    }

好看请赞,养成习惯:) ,作者:靠谱杨​, 转载请注明原文链接​

关于笔者:​​ 我的主页 ​​

文章同步51CTO,可以帮忙踩一踩 ~ ​​我的51CTO博客​​

更多日常分享尽在我的VX公众号:小杨的挨踢IT生活

Java手写实现栈【数据结构与算法】_数组



举报

相关推荐

0 条评论