LQueue.java 866 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. public class LQueue<T>
  2. {
  3. public static class MyException extends RuntimeException
  4. {
  5. private static final long serialVersionUID = -244806549310243756L;
  6. public MyException()
  7. {
  8. super();
  9. }
  10. public MyException(String message)
  11. {
  12. super(message);
  13. }
  14. }
  15. private Node front, end;
  16. public LQueue()
  17. {
  18. front = end = null;
  19. }
  20. public void enqueue(T item)
  21. {
  22. Node newEnd = new Node(item);
  23. if (isEmpty())
  24. {
  25. front = newEnd;
  26. }
  27. else
  28. {
  29. end.next = newEnd;
  30. }
  31. end = newEnd;
  32. }
  33. public T dequeue()
  34. {
  35. if (isEmpty())
  36. {
  37. throw new MyException();
  38. }
  39. try
  40. {
  41. return front.item;
  42. }
  43. finally
  44. {
  45. front = front.next;
  46. }
  47. }
  48. public boolean isEmpty()
  49. {
  50. return front == null;
  51. }
  52. private class Node
  53. {
  54. public T item;
  55. public Node next;
  56. public Node(T item)
  57. {
  58. this.item = item;
  59. this.next = null;
  60. }
  61. }
  62. }