JAVA单链表模拟栈

一、定义节点和链表

class singlelinked{
    private Node head = new Node(-1,-1);
    public singlelinked() {
    }
    public Node getHead() {
        return head;
    }
    //Determine if it is empty
    public boolean isNull(){
        if (head.getNext() == null){
            return true;
        }else{
            return false;
        }
    }
    //Inserts data to the end of the list
    public void add(Node node) {
        if (head.getNext() == null){
            head.setNext(node);
            return;
        }
        Node temp = head;
        while (true){
            if (temp.getNext() == null){
                break;
            }
            temp = temp.getNext();
        }
        temp.setNext(node);
        return;
    }
    //Delete the data for the corresponding number
    public int del(){
        if (head.getNext() == null){
            throw new RuntimeException("error");
        }
        Node node = head;
        while (true){
            if (node.getNext().getNext() == null){
                break;
            }
            node = node.getNext();
        }
        int value = node.getNext().getNum();
        node.setNext(node.getNext().getNext());
        return value;
    }

    //Traversing the list
    public void list(){
        if (head.getNext() == null){
            System.out.println("error!");
            return;
        }
        Node temp = head.getNext();
        while (true){
            System.out.println(temp);
            if (temp.getNext() == null){
                break;
            }
            temp = temp.getNext();
        }
    }
}

class Node{
    private int no;
    private int num;
    private Node next;
    public void setNext(Node next) {
        this.next = next;
    }
    public Node getNext() {
        return next;
    }
    @Override
    public String toString() {
        return "[num="+num+"]";
    }
    public void setNo(int no) {
        this.no = no;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public Node(int no, int num) {
        this.no = no;
        this.num = num;
    }
    public int getNo() {
        return no;
    }
    public int getNum() {
        return num;
    }
}

二、定义栈

class Arraystack{
    private singlelinked singlelinked = new singlelinked();
    private int top = -1;
    public Arraystack() {
    }
    //Determines whether the stack is empty
    public boolean isNull(){
        return singlelinked.isNull();
    }
    //push
    public void push(int value){
        top++;
        Node node = new Node(top, value);
        singlelinked.add(node);
    }
    //pop
    public int pop(){
        int value = singlelinked.del();
        return value;
    }
    //Traversal stack
    public void list(){ singlelinked.list();}
}

2.测试代码

public static void main(String[] args){
	Arraystack arraystack = new Arraystack();
	arraystack.push(1);
	arraystack.push(2);
	arraystack.push(3);
	arraystack.push(4);
    arraystack.list();
    System.out.println("-------------");
    System.out.println(arraystack.pop());
    System.out.println(arraystack.pop());
    arraystack.list();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值