Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package DoublyLinkedList;
  2.  
  3. import java.util.NoSuchElementException;
  4.  
  5. public class DLQueue {
  6.  
  7. private Node first;
  8. private Node last;
  9.  
  10. public DLQueue(){
  11. first = null;
  12. last = null;
  13. }
  14.  
  15. /**
  16. * adds an element to the stack
  17. * @param data - the element to add
  18. */
  19. public void push(Object data){
  20. Node newNode = new Node();
  21. newNode.data = data;
  22. newNode.next = first;
  23. first = newNode;
  24. }
  25.  
  26. /**
  27. * returns the last element added to the stack
  28. * @return an element removed, of the type Object
  29. */
  30. public Object pop(){
  31. if(first == null){throw new NoSuchElementException();}
  32. Object temp = first.data;
  33. first = first.next;
  34. return temp;
  35. }
  36.  
  37. /**
  38. * returns the last element added to the stack
  39. * @return an element of the type Object
  40. */
  41. public Object peek(){
  42. if(first == null){throw new NoSuchElementException();}
  43. return first.data;
  44. }
  45.  
  46. /**
  47. * adds an element to the list
  48. * @return
  49. */
  50. public void add(Object data){
  51. Node newNode = new Node();
  52. newNode.data = data;
  53. newNode.next = first;
  54. newNode.previous = null;
  55. if (first == null) {
  56. last = newNode;
  57. }
  58. else {
  59. first.previous = newNode;
  60. }
  61. first = newNode;
  62.  
  63. }
  64.  
  65. /**
  66. * removes an element in the current position of the iterator
  67. * @return - the element that is removed
  68. */
  69. public Object remove(){
  70. if(first==null){
  71. throw new NoSuchElementException();
  72. }
  73. else if(first == last){
  74. last = null;
  75. }
  76. Object temp = first.data;
  77. first = first.next;
  78. return temp;
  79. }
  80.  
  81.  
  82.  
  83. class Node{
  84. public Object data;
  85. public Node next;
  86. public Node previous;
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement