JAVA实现动态栈

109 篇文章 0 订阅
14 篇文章 1 订阅

在数据结构中,栈是一种极其实用的结构

所以,有必要将其掌握,栈的特点是“先入后出”,只能从一端进,一端出。
栈

package stack;//包名

interface Stack {//接口,一些可能会用的方法
void stackPush(Object obj);//入栈
void stackPop();//出栈
int stackSize();//获取栈中元素数量
Object stackTop();//获取栈顶元素
void printLink();//辅助方法,打印栈中所有元素
}

class Factory {// 工厂模式实例化StackImpl类
private Factory() {
}

public static Stack getLinkInstance() {
    return new StackImpl();
    }
}

class StackImpl implements Stack {// 继承了Stack接口的方法,通过向上转型,产生实例化对象
private int size = 0;// Stack入栈元素的数量
private Node first;// 栈底,
private Node last;// 栈顶

private class Node {// 产生节点的内部类
    private Object item;// 存放入栈元素
    private Node next;// 放下一次要入栈的节点

    private Node(Object item, Node next) {// 构造方法
        this.item = item;
        this.next = next;
    }
}

@Override
public void stackPush(Object obj) {// 入栈方法
    Node tmp = this.first;
    Node newnode = new Node(obj, null);
    this.last = newnode;
    if (this.first == null) {// 第一次入栈,设栈底
        this.first = newnode;
    } else {
        while (null != tmp.next) {// 非第一次入栈,遍历找栈顶,入栈
            tmp = tmp.next;
        }
        tmp.next = newnode;
    }
    this.size++;// 记得扩充元素数量
}

@Override
public void stackPop() {// 出栈
    Node tmp = this.first;
    while (null != tmp.next.next) {// 遍历栈,找离栈顶一个节点的节点
        tmp = tmp.next;
    }
    this.last = tmp;// 重设栈顶
    tmp.next = null;
    this.size--;
}

@Override
public int stackSize() {// 获取当前栈中节点数(元素数量)
    return this.size;
}

@Override
public Object stackTop() {// 获取栈顶元素
    return this.last.item;
}

@Override
public void printLink() {// 辅助方法,查看当前栈中的所有元素
    Node tmp = this.first;
    while (null != tmp) {
        System.out.println(tmp.item);
        tmp = tmp.next;
        }
    }
}

public class Test {
public static void main(String[] args) {
    Stack stack = Factory.getLinkInstance();//调用工厂类get方法,产生一个StackImpl类实例,向上转型实现Stack接口

    //+++++++++++++++++++++++
    //测试入栈
    stack.stackPush("1");
    stack.stackPush("2");
    stack.stackPush("3");
    stack.stackPush("4");
    stack.stackPush("5");
    stack.stackPush("6");
    stack.printLink();
    System.out.println("++++++++++++++++++++++++++");
    //+++++++++++++++++++++++++++++++++
    //测试出栈
    // link.stackPop();
    stack.printLink();
    System.out.println("++++++++++++++++++++++++++");
    //+++++++++++++++++++++++++++++++++
    //测试获取栈顶元素和元素数量
    System.out.println(stack.stackTop());
    System.out.println(stack.stackSize());
}

}

更新

修正public void stackPop() {// 出栈 方法的一处错误

@Override
public void stackPop() {// 出栈
    Node tmp = this.first;
    while (null != tmp.next.next) {// 遍历栈,找离栈顶一个节点的节点
        tmp = tmp.next;
    }
    this.last = tmp;// 重设栈顶
    tmp.next = null;
    this.size--;
}

这里忽略了栈中只有一个元素的情况,所以新增栈中只有一个元素时的判断

    @Override
public void stackPop() {// 出栈
    Node tmp = this.first;
    if(null ==tmp.next) {//当栈中只有一个元素时,直接清空栈
        this.first = null;
        this.last = null;
        this.size = 0;
        return;
    }
    while (null != tmp.next.next) {// 遍历栈,找离栈顶一个节点的节点
        tmp = tmp.next;
    }
    this.last = tmp;// 重设栈顶
    tmp.next = null;
    this.size--;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,停车场管理系统可以作为一个进出流量监控的例子。下面是Java实现进出流量监控的步骤: 1.定义一个类,包括的基本操作,如入、出、获取顶元素等。 2.定义一个计数器类,用于记录进出的流量。 3.在入和出操作中,分别调用计数器类的增加和减少方法,记录进出的流量。 4.在需要监控进出流量的地方,调用计数器类的获取流量方法,获取当前的进出流量。 下面是一个简单的Java代码示例: ```java public class Stack { private int[] data; private int top; public Stack(int size) { data = new int[size]; top = -1; } public boolean push(int value) { if (top == data.length - 1) { return false; } data[++top] = value; return true; } public int pop() { if (top == -1) { return -1; } return data[top--]; } public int peek() { if (top == -1) { return -1; } return data[top]; } } public class Counter { private int inCount; private int outCount; public void increaseInCount() { inCount++; } public void increaseOutCount() { outCount++; } public int getFlow() { return inCount - outCount; } } // 在入和出操作中调用计数器类的增加和减少方法 public class ParkingLot { private Stack stack; private Counter counter; public ParkingLot(int size) { stack = new Stack(size); counter = new Counter(); } public boolean push(int value) { boolean result = stack.push(value); if (result) { counter.increaseInCount(); } return result; } public int pop() { int result = stack.pop(); if (result ! -1) { counter.increaseOutCount(); } return result; } public int getFlow() { return counter.getFlow(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值