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 }
#学习笔记,如有谬误,敬请指正。#