Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. /*
  2. A double linked list that stores EzImage objects.
  3.  
  4. CS 201, Winter 2020
  5. date: 26 January 2020
  6. */
  7.  
  8. public class ImageDLList {
  9. // instance variables
  10. private Node head;
  11. private Node tail;
  12. private int size;
  13.  
  14. public ImageDLList() {
  15. this.head = null;
  16. this.tail = null;
  17. this.size = 0;
  18. }
  19.  
  20. public boolean addToHead(EzImage im) {
  21. Node newNode = new Node(im);
  22. if (this.size > 0) {
  23. newNode.next = this.head;
  24. this.head.previous = newNode;
  25. }
  26. else {
  27. this.tail = newNode;
  28. }
  29. this.head = newNode;
  30. this.size++;
  31. return true;
  32. }
  33.  
  34. public boolean addToTail(EzImage im) {
  35. Node newNode = new Node(im);
  36. if (size > 0) {
  37. newNode.previous = this.tail;
  38. this.tail.next = newNode;
  39. }
  40. this.tail = newNode;
  41. this.size++;
  42. return true;
  43. }
  44.  
  45. public EzImage remove(EzImage im) {
  46. // Special case: if the list is empty, return null
  47. if (this.head == null) {
  48. return null;
  49. }
  50.  
  51. Node current = this.head;
  52. Node previous = current;
  53.  
  54. // Traverse the list until you either find the item or reach the end.
  55. while (current != null && !(current.im.equals(im))) {
  56. previous = current;
  57. current = current.next;
  58. }
  59.  
  60. if (current != null) {
  61. previous.next = current.next;
  62. current.next.previous = previous;
  63. this.size--;
  64.  
  65. // Special case: if the node we're removing is the head, update the head to refer to the next node. This will also take care of the case where we remove the last node in the list.
  66. if (this.head == current) {
  67. this.head = this.head.next;
  68. }
  69.  
  70. if (this.tail == current) {
  71. this.tail = this.tail.previous;
  72. }
  73. // return the data at the removed node.
  74. return current.im;
  75. } else {
  76. return null;
  77. }
  78. }
  79.  
  80. public int indexOf(EzImage im) {
  81. int index = 0;
  82. Node current = this.head;
  83. Node previous = current;
  84. while (current != null && !(current.im.equals(im))) {
  85. previous = current;
  86. current = current.next;
  87. index++;
  88. }
  89. return index;
  90. }
  91.  
  92. public void traverseForward(EzImage base) {
  93. Node current = this.head;
  94. Node previous = current;
  95. EzImage layer = current.im;
  96. for(int i = 0; i < this.size; i++){
  97. int r, g, b;
  98.  
  99. for (int j=0; j<layer.getHeight(); j++) {
  100. for (int k=0; k<layer.getWidth(); k++) {
  101.  
  102. r = layer.getRedPixel(j, k);
  103. g = layer.getGreenPixel(j, k);
  104. b = layer.getBluePixel(j, k);
  105. if (g < 250) {
  106. base.setRedPixel(j, k, r);
  107. base.setGreenPixel(j, k, g);
  108. base.setBluePixel(j, k, b);
  109. }
  110. }
  111. }
  112. if(current.next != null){
  113. previous = current;
  114. current = current.next;
  115. layer = current.im;
  116. }
  117. }
  118. base.show("Forward Result");
  119. }
  120.  
  121. public void traverseBackward(EzImage base) {
  122. Node current = this.tail;
  123. Node next = current;
  124. EzImage layer = current.im;
  125. for(int i = 0; i < this.size; i++){
  126. int r, g, b;
  127.  
  128. for (int j=0; j<layer.getHeight(); j++) {
  129. for (int k=0; k<layer.getWidth(); k++) {
  130.  
  131. r = layer.getRedPixel(j, k);
  132. g = layer.getGreenPixel(j, k);
  133. b = layer.getBluePixel(j, k);
  134. if (g < 250) {
  135. base.setRedPixel(j, k, r);
  136. base.setGreenPixel(j, k, g);
  137. base.setBluePixel(j, k, b);
  138. }
  139. }
  140. }
  141. if(current.previous != null){
  142. next = current;
  143. current = current.previous;
  144. layer = current.im;
  145. }
  146. }
  147. base.show("Backwards Result");
  148. }
  149.  
  150.  
  151. /**
  152. Node inner class
  153. */
  154. public class Node {
  155. // instance variables
  156. private EzImage im;
  157. private Node previous;
  158. private Node next;
  159. public Node() {
  160. this.im = null;
  161. this.previous = null;
  162. this.next = null;
  163. }
  164.  
  165. public Node(EzImage d) {
  166. this.im = d;
  167. this.previous = null;
  168. this.next = null;
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement