腾讯云授权服务中心
五万用户的选择,您身边的云计算顾问
发布日:2022-01-19 12:58 阅读数:
class Node<T> { public T data; public Node next; public Node(T data) { this.data = data; } } public class MyQueue<T> { private Node<T> head; private Node<T> rear; private int size; public MyQueue() { size = 0; } public void pushBack(T element) { Node newNode = new Node(element); if (isEmpty()) { head = newNode; } else { rear.next = newNode; } rear = newNode; size++; } public boolean isEmpty() { return head == null; } public T popFront() { if (isEmpty()) { throw new NullPointerException("队列没有数据"); } else { Node<T> node = head; head = head.next; size--; return node.data; } } public void dispaly() { Node temp = head; while (temp != null) { System.out.print(temp.data +" -> "); temp = temp.next; } System.out.println(""); } }
测试代码如下:
public class MyStackTest { public static void main(String[] args) { MyStack<Integer> myStack = new MyStack<>(); myStack.push(1); myStack.push(2); myStack.push(3); myStack.push(4); myStack.display(); System.out.println(myStack.pop()); myStack.display(); } }
运行结果:
1 -> 2 -> 3 -> 1 2 -> 3 -> 2 3 ->
编辑:航网科技 来源:腾讯云
本文版权归原作者所有 转载请注明出处
Copyright © 2011-2020 www.hangw.com. All Rights Reserved 深圳航网科技有限公司 版权所有 增值电信业务经营许可证:粤B2-20201122 - 粤ICP备14085080号
微信扫一扫咨询客服
全国免费服务热线
0755-36300002