单链表实现栈和队列

package 算法;

/**
 * 单链表实现 栈和队列
 */
public class test {
    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            value = data;
        }
    }
    //单项链表实现栈
    public static class LinkedStack{
        public Node head =null;

        public void pull(Node value){
            if(head==null){
                //如果没有就直接装
                head = value;
            }else{
                //如果有就把新的换成头
                value.next = head;
                head = value;
            }
        }
        public Node pop( ){
            if (head == null) {
                return null;
            }
            Node rt =head;
            if(head.next!=null){
                head = head.next;
                rt.next =null;
            }else{
                head =null;
            }
            return rt;
        }
    }
    //单项链表实现队列 先进后出
    public static class LinkedQueue{
        public Node head =null;
        public Node tail = null;

        public void pull(Node value){
            if(tail==null){
                //如果没有就直接装
                head = value;
                tail = value;
            }else{
                //如果有就把新的换成头
                tail.next = value;
                tail = value;
            }

        }
        public Node poll( ){
            Node rt = null;
            if (head != null) {
                rt = head;
                head = head.next;
                rt.next =null;
                if (head == null) {
                    tail = null;
                }
            }
            return rt;
        }
    }

    public static void main(String[] args) {
        //栈测试
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        LinkedStack linkedStack = new LinkedStack();
        linkedStack.pull(n1);
        linkedStack.pull(n2);
        linkedStack.pull(n3);
        linkedStack.pull(n4);
        System.out.println(linkedStack.pop().value);
        System.out.println(linkedStack.pop().value);
        System.out.println(linkedStack.pop().value);
        System.out.println(linkedStack.pop().value);
        System.out.println(linkedStack.pop());

        linkedStack.pull(n1);
        System.out.println(linkedStack.pop().value);
        linkedStack.pull(n2);
        linkedStack.pull(n3);
        System.out.println(linkedStack.pop().value);
        linkedStack.pull(n4);
        System.out.println(linkedStack.pop().value);
        System.out.println(linkedStack.pop().value);
        System.out.println(linkedStack.pop());


        //队列测试
        LinkedQueue linkedQueue = new LinkedQueue();
        linkedQueue.pull(n1);
        linkedQueue.pull(n2);
        linkedQueue.pull(n3);
        linkedQueue.pull(n4);
        System.out.println(linkedQueue.poll().value);
        System.out.println(linkedQueue.poll().value);
        System.out.println(linkedQueue.poll().value);
        System.out.println(linkedQueue.poll().value);
        System.out.println(linkedQueue.poll());

        linkedQueue.pull(n1);
        System.out.println(linkedQueue.poll().value);
        linkedQueue.pull(n2);
        linkedQueue.pull(n3);
        System.out.println(linkedQueue.poll().value);
        linkedQueue.pull(n4);
        System.out.println(linkedQueue.poll().value);
        System.out.println(linkedQueue.poll().value);
        System.out.println(linkedQueue.poll());

    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值