Advertisement
Guest User

asdfasdf

a guest
Oct 1st, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. public class Deque<Item> implements Iterable<Item> {
  2.  
  3. private int size; // number of items
  4. private Node first; // top of stack
  5. private Node last;
  6.  
  7. private class Node {
  8.  
  9. Item item;
  10. Node next;
  11. Node previous;
  12.  
  13. public Node(Item itemX) {
  14. item = itemX;
  15. }
  16.  
  17. public Node() {
  18. }
  19. }
  20.  
  21. public Deque() {
  22. first = null;
  23. last = null;
  24. size = 0;
  25. }
  26.  
  27. public boolean isEmpty() {
  28. return first == null;
  29. }
  30.  
  31. public int size() {
  32. return size;
  33. }
  34.  
  35. public void pushRight(Item item) {
  36. //adds item to top of stack
  37. Node newNode = new Node(item);
  38. newNode.next = null;
  39. if (isEmpty()) {
  40. first = newNode;
  41. } else {
  42. last.next = newNode;
  43. newNode.previous = last;
  44. }
  45. last = newNode;
  46.  
  47. }
  48. public void changeLeft(int k, Item newItem){
  49. int i = 1;
  50. Node current = first;
  51.  
  52. while(i < k && current != null){
  53. current = current.next;
  54. i++;
  55. }
  56. if(current != null){
  57. current.item = newItem;
  58. }
  59. else{
  60. System.out.println("Linkedlist out of bounds");
  61. }
  62.  
  63. }
  64.  
  65. public void changeRight(int k, Item newItem){
  66. int size2 = size;
  67. Node current = last;
  68.  
  69. while(size2 > k && current != null){
  70. current = current.previous;
  71. size--;
  72. }
  73. if(current != null){
  74. current.item = newItem;
  75. }
  76. else{
  77. System.out.println("Linkedlist out of bounds");
  78. }
  79. }
  80.  
  81.  
  82.  
  83.  
  84. public void pushLeft(Item item) {
  85. //adds item to bottom of stack
  86.  
  87. Node newNode = new Node(item);
  88.  
  89. if (isEmpty()) {
  90. last = newNode;
  91. } else {
  92. first.previous = newNode;
  93. }
  94. newNode.next = first;
  95. first = newNode;
  96.  
  97. }
  98.  
  99. public Item popLeft() {
  100. Item t = first.item;
  101. if (first.next == null) {
  102. last = null;
  103. } else {
  104. first.next.previous = null;
  105. }
  106. first = first.next;
  107. return t;
  108. }
  109.  
  110. public Item popRight() {
  111. Item t = last.item;
  112. if (first.next == null) {
  113. first = null;
  114. } else {
  115. last.previous.next = null;
  116. }
  117. last = last.previous;
  118. return t;
  119. }
  120.  
  121. public void printDeque() {
  122. Node tmpNode = first;
  123.  
  124. while (tmpNode != null) {
  125. System.out.print(tmpNode.item + " -> ");
  126. tmpNode = tmpNode.next;
  127. }
  128.  
  129. System.out.print("null");
  130. }
  131.  
  132. @Override
  133. public Iterator<Item> iterator() {
  134. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement