单链表实现栈和队列

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());

    }
}

猜你喜欢

转载自blog.csdn.net/u010191034/article/details/120969533