Java链表模拟栈---简单版

Java链表模拟栈

1.定义一个节点

public class Node {
    int no;
    Node next;
    
    public Node() {
    }
    
    public Node(int no) {
        this.no = no;
    }
    
    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                '}';
    }
}

2.定义一个链表

public class Linked {
    private Node top=new Node();
    int maxSize;
    int[] stack;

    public Linked(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }
    //栈满
    public boolean isFull(){
        return length(top)==maxSize;
    }
    //栈空
    public boolean isEmpty(){
        return top.next==null;
    }
    //求出节点个数
    public int length(Node node) {
        if (node==null){
            return 0;
        }
        Node temp =top;
        int linkedLength =0;
        while(temp.next != null){
            linkedLength++;
            temp = temp.next;
        }
        return linkedLength;
    }
	//入栈
    public void push(Node node){
        if (isFull()){
            System.out.printf("栈满,%d无法入栈",node.no);
            return;
        }

        if(top.next== null){// 第一个节点的插入
            top.next=node;
            return;
        }
	//引入临时变量temp
        Node temp = top.next;
        if (node.no==temp.no){
            System.out.printf("%d已经存在",node.no);
            System.out.println("");
            return;
        }
        top.next=node;
        node.next=temp;

    }
    //出栈
    public void pop(){
        if(isEmpty()){
            System.out.println("栈空。。。");
            return;
        }
        System.out.println("节点为:"+top.next.no);
        top =top.next;
    }
    //显示栈
    public void list(){
        if(isEmpty()){
            System.out.println("栈空。。。");
            return;
        }
        Node temp = top;
        while(temp.next != null){
            System.out.println("节点为:"+temp.next.no);
            temp = temp.next;
        }
    }
    //显示栈顶元素
    public void peek(){
        if(isEmpty()){
            System.out.println("栈空。。。");
            return;
        }
            System.out.println("栈顶元素为:"+top.no);
    }
}

3.写一个测试类

public class LinkedDemo {
    public static void main(String[] args) {
        Node p1 = new Node(3);
        Node p2 = new Node(1);
        Node p3 = new Node(5);
        Node p4 = new Node(55);
        Node p5 = new Node(566);
        Linked stack = new Linked(4);
        stack.push(p1);
        stack.push(p2);
        stack.push(p3);
        stack.push(p4);
        stack.push(p5);
        System.out.println("-----");
        stack.pop();
        System.out.println("-----");
        stack.list();
        System.out.println("----");
        stack.peek();
        System.out.println("节点个数:"+stack.length(p1));
    }
}

4.输出结果

栈满,566无法入栈-----
节点为:55
-----
节点为:5
节点为:1
节点为:3
----
栈顶元素为:55
节点个数:3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值