zoukankan      html  css  js  c++  java
  • java实现栈和队列

     1 class Node {
     2     int val;
     3     Node next;
     4     
     5     Node(int x) {
     6         val = x;
     7         next = null;
     8     }
     9 }
    10 
    11 class Stack {
    12     Node top;
    13     
    14     public Node peek() {
    15         if (top != null) {
    16             return top;
    17         }
    18         return null;
    19     }
    20     
    21     public Node pop() {
    22         if (top == null) {
    23             return null;
    24         } else {
    25             Node temp = new Node(top.val);
    26             top = top.next;
    27             return temp;
    28         }
    29     }
    30     public void push(Node n) {
    31         if (n != null) {
    32             n.next = top;
    33             top = n;
    34         }
    35     }
    36 }
    37 
    38 class Queue {
    39     Node first, last;
    40     
    41     public void enqueue(Node n) {
    42         if (first == null) {
    43             first = n;
    44             last = first;
    45         } else {
    46             last.next = n;
    47             last = n;
    48         }
    49     }
    50     
    51     public Node dequeue() {
    52         if (first == null) {
    53             return null;
    54         } else {
    55             Node temp = new Node(first.val);
    56             first = first.next;
    57             return temp;
    58         }
    59     } 
    60 }
  • 相关阅读:
    Linux各目录的意义
    LinuxVIM编辑器用法
    Linux自动同步时间
    bash的基本特性
    shell-homeworkone
    shell
    笔记
    Python-1-Day
    Linux使用BIND提供域名解析服务
    Linuxautofs自动挂载服务
  • 原文地址:https://www.cnblogs.com/happyhacking/p/4351787.html
Copyright © 2011-2022 走看看