Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /**
  2. * Course: EECS 114 Fall 2015
  3. *
  4. * First Name: Kevin
  5. * Last Name: Ngo
  6. * Lab Section: 1A
  7. * email address: kngo5@uci.edu
  8. *
  9. *
  10. * Assignment: assn2
  11. * Filename : MyQueue.java
  12. *
  13. * I hereby certify that the contents of this file represent
  14. * my own original individual work. Nowhere herein is there
  15. * code from any outside resources such as another individual,
  16. * a website, or publishings unless specifically designated as
  17. * permissible by the instructor or TA.
  18. */
  19.  
  20.  
  21. package assn2;
  22.  
  23. public class MyQueue<T> {
  24.  
  25. SimpleList<T> list;
  26.  
  27. /*SimpleList as underlying data structure*/
  28. public MyQueue(SimpleList<T> list)
  29. {
  30. this.list = list;
  31.  
  32. }
  33.  
  34. /*Returns true of the queue is empty*/
  35. public boolean isEmpty()
  36. {
  37. if(list.size() == 0)
  38. {
  39. return true;
  40. }
  41. return false;
  42. }
  43.  
  44. /*Inserts a value onto the rear of the queue*/
  45. public void push(T value)
  46. {
  47. list.insertAtPos(list.size() -1, value);
  48. }
  49. /*Remove the front item*/
  50. public void pop()
  51. {
  52. try
  53. {
  54. if(list.size() == 0)
  55. {
  56. String error = "pop() on MyQueue of size == 0";
  57. throw new QueueUnderflowException(error);
  58. }
  59. }
  60. catch(QueueUnderflowException e)
  61. {
  62. e.printStackTrace();
  63. }
  64. list.removeAt(0);
  65. }
  66.  
  67. /*Returns the front item in the queue*/
  68. public T front()
  69. {
  70. try
  71. {
  72. if(list.size() == 0)
  73. {
  74. String error = "front() on MyQueue of size == 0";
  75. throw new QueueUnderflowException(error);
  76. }
  77. }
  78. catch(QueueUnderflowException e)
  79. {
  80. e.printStackTrace();
  81. }
  82. return list.getAt(0);
  83. }
  84.  
  85. public int size()
  86. {
  87. return list.size();
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement