前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java - 手写队列/栈

java - 手写队列/栈

作者头像
夹胡碰
发布2021-04-15 10:08:06
5350
发布2021-04-15 10:08:06
举报
文章被收录于专栏:程序猿~

话不多少,直接上代码

1. 手写队列

代码语言:javascript
复制
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);
    }
}
  • 输出
代码语言:javascript
复制
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. 手写栈

代码语言:javascript
复制
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);
    }
}
  • 输出
代码语言:javascript
复制
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)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 手写队列
  • 2. 手写栈
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档