java - 手写队列/栈

话不多少,直接上代码

1. 手写队列

package com.jiafupeng.test;

/**
 * @author jiafupeng
 * @desc
 * @create 2021/4/10 15:59
 * @update 2021/4/10 15:59
 **/
public class MyQueue {

    private ListNode head;

    private ListNode tail;

    private static class ListNode{
        private int value;
        private ListNode pre;
        private ListNode next;

        public ListNode(int value, ListNode pre, ListNode next) {
            this.value = value;
            this.pre = pre;
            this.next = next;
        }
    }

    public void offer(int val){
        if(head == null){
            head = tail = new ListNode(val, null, null);
        } else {
            tail.next = new ListNode(val, tail, null);
            tail = tail.next;
        }
    }

    public int take(){
        if(tail == null){
            throw new RuntimeException("no node to take");
        } else {
            ListNode pop = head;
            head = head.next;
            if(head == null){
                tail = head;
            } else {
                head.pre = null;
            }
            return pop.value;
        }
    }

    @Override
    public String toString(){
        StringBuffer ss = new StringBuffer();
        ListNode cur = head;
        while(cur != null){
            ss.append(cur.value).append("=>");
            cur = cur.next;
        }
        ss.append("null");
        return ss.toString();
    }

    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);

        System.out.println("toString: " + myQueue);

        System.out.println("take: " + myQueue.take());
        System.out.println("toString: " + myQueue);
        System.out.println("take: " + myQueue.take());
        System.out.println("toString: " + myQueue);
        System.out.println("take: " + myQueue.take());
        System.out.println("toString: " + myQueue);
        System.out.println("take: " + myQueue.take());
        System.out.println("toString: " + myQueue);
    }
}
  • 输出
toString: 1=>2=>3=>null
take: 1
toString: 2=>3=>null
take: 2
toString: 3=>null
take: 3
toString: null
Exception in thread "main" java.lang.RuntimeException: no node to take
    at com.jiafupeng.test.MyQueue.take(MyQueue.java:38)
    at com.jiafupeng.test.MyQueue.main(MyQueue.java:77)

2. 手写栈

package com.jiafupeng.test;

/**
 * @author jiafupeng
 * @desc
 * @create 2021/4/10 15:35
 * @update 2021/4/10 15:35
 **/
public class MyStack {

    private ListNode head;

    private ListNode tail;

    private static class ListNode{
        private int value;
        private ListNode pre;
        private ListNode next;

        public ListNode(int value, ListNode pre, ListNode next) {
            this.value = value;
            this.pre = pre;
            this.next = next;
        }
    }

    public void push(int val){
        if(head == null){
            head = tail = new ListNode(val, null, null);
        } else {
            tail.next = new ListNode(val, tail, null);
            tail = tail.next;
        }
    }

    public int pop(){
        if(tail == null){
            throw new RuntimeException("no node to pop");
        } else {
            ListNode pop = tail;
            tail = tail.pre;
            if(tail == null){
                head = tail;
            } else {
                tail.next = null;
            }
            return pop.value;
        }
    }

    @Override
    public String toString(){
        StringBuffer ss = new StringBuffer();
        ListNode cur = head;
        while(cur != null){
            ss.append(cur.value).append("=>");
            cur = cur.next;
        }
        ss.append("null");
        return ss.toString();
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);

        System.out.println("toString: " + myStack);

        System.out.println("pop: " + myStack.pop());
        System.out.println("toString: " + myStack);
        System.out.println("pop: " + myStack.pop());
        System.out.println("toString: " + myStack);
        System.out.println("pop: " + myStack.pop());
        System.out.println("toString: " + myStack);
        System.out.println("pop: " + myStack.pop());
        System.out.println("toString: " + myStack);
    }
}
  • 输出
toString: 1=>2=>3=>null
pop: 3
toString: 1=>2=>null
pop: 2
toString: 1=>null
pop: 1
toString: null
Exception in thread "main" java.lang.RuntimeException: no node to pop
    at com.jiafupeng.test.MyStack.pop(MyStack.java:38)
    at com.jiafupeng.test.MyStack.main(MyStack.java:77)

你可能感兴趣的:(java - 手写队列/栈)