Advertisement
Guest User

Untitled

a guest
May 1st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.linkedList.cs61;
  2.  
  3. import eg.edu.alexu.csd.datastructure.linkedList.ILinkedList;
  4. // TODO: Auto-generated Javadoc
  5.  
  6. /**
  7. * The Class DoublyLinkedList.
  8. */
  9. public class DoublyLinkedList implements ILinkedList {
  10.  
  11. /** The len. */
  12. private int len = 0;
  13.  
  14. /** The head. */
  15. private Node head = new Node(null, null, null);
  16.  
  17. /**
  18. * The Class Node.
  19. */
  20. private class Node {
  21.  
  22. /** The data. */
  23. private Object data;
  24.  
  25. /** The next. */
  26. private Node next;
  27.  
  28. /** The prev. */
  29. private Node prev;
  30.  
  31. /**
  32. * Instantiates a new node.
  33. *
  34. * @param data
  35. * the data
  36. * @param next
  37. * the next
  38. * @param prev
  39. * the prev
  40. */
  41. public Node(Object data, Node next, Node prev) {
  42. this.data = data;
  43. this.next = next;
  44. this.prev = prev;
  45. }
  46. }
  47.  
  48. /*
  49. * (non-Javadoc)
  50. *
  51. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#add(int,
  52. * java.lang.Object)
  53. */
  54. public void add(int index, Object element) {
  55. if (index > len + 1 || index < 0) {
  56. throw new RuntimeException("add function");
  57. }
  58. else {
  59.  
  60. Node link = new Node(null, head, null);
  61. int counter = 0;
  62. if (index != 0 && head.next == null) {
  63. throw new RuntimeException("add function");
  64. }
  65. if (index == 0 && head.next == null) {
  66. head.next = link;
  67. link.data = element;
  68. link.next = null;
  69. link.prev = head;
  70. len++;
  71. } else {
  72. while (counter <= index) {
  73. counter++;
  74. link = link.next;
  75. }
  76. if (counter > index && head.next != null) {
  77. Node temp = new Node(element, link.next, link);
  78. link.next = temp;
  79. len++;
  80. }
  81.  
  82. }
  83. }
  84.  
  85. }
  86.  
  87. /*
  88. * (non-Javadoc)
  89. *
  90. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#add(java.lang.
  91. * Object)
  92. */
  93. public void add(Object element) {
  94. int length = size(); // It's the length of the list
  95. int index = 0; // It's the counter to reach the tail
  96. Node link = new Node(null, head, null);
  97. while (index <= length) {
  98. link = link.next;
  99. index++;
  100. }
  101. Node temp = new Node(element, null, link);
  102. link.next = temp;
  103. len++;
  104. }
  105.  
  106. /*
  107. * (non-Javadoc)
  108. *
  109. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#get(int)
  110. */
  111. public Object get(int index) {
  112. if (len == 0 || index < 0 || index > len) { // It checks if the list is
  113. // empty
  114. throw new RuntimeException("get function"); // or the index is
  115. } // greater than the size
  116. else { // or the index is negative
  117.  
  118. Object getter;
  119. int counter = -1;
  120. Node link = new Node(null, head, null);
  121. while (counter < index) { // There's no need to check if the next is
  122. // null
  123. (link.next).prev = link;
  124. link = link.next; // because it's checked at first that the
  125. // index is less
  126. counter++; // than the length
  127. }
  128. getter = (link.next).data;
  129. return getter;
  130. }
  131. }
  132.  
  133. /*
  134. * (non-Javadoc)
  135. *
  136. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#set(int,
  137. * java.lang.Object)
  138. */
  139. public void set(int index, Object element) {
  140. if (head.next == null || index < 0 || index >= len) {
  141. throw new RuntimeException("set function");
  142. }
  143. else {
  144. int counter = -1; // It's initializes with -1 because the index
  145. Node link = new Node(null, head, null); // is initialized with 0
  146. while (counter < index) { // With that loop all it does is reaching
  147. // the required
  148. counter++; // index
  149. (link.next).prev = link;
  150. link = link.next;
  151. }
  152. (link.next).data = element;
  153. }
  154. }
  155.  
  156. /*
  157. * (non-Javadoc)
  158. *
  159. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#clear()
  160. */
  161. public void clear() {
  162. len = 0;
  163. head.next = null;
  164. }
  165.  
  166. /*
  167. * (non-Javadoc)
  168. *
  169. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#isEmpty()
  170. */
  171. public boolean isEmpty() {
  172.  
  173. return size() == 0;
  174. }
  175.  
  176. /*
  177. * (non-Javadoc)
  178. *
  179. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#remove(int)
  180. */
  181. public void remove(int index) {
  182. if (head.next == null || index < 0 || index >= len) {
  183. throw new RuntimeException("remove function");
  184. }
  185.  
  186. else {
  187. Node link = new Node(null, head, null);
  188. int counter = 0;
  189. while (counter <= index) {
  190. // (link.next).prev = link ;
  191. link = link.next;
  192. counter++;
  193. }
  194.  
  195. link.next = (link.next) != null ? (link.next).next : link.next;
  196. if (link.next != null) {
  197. (link.next).prev = link;
  198. }
  199. len--;
  200. }
  201. }
  202.  
  203. /*
  204. * (non-Javadoc)
  205. *
  206. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#size()
  207. */
  208. public int size() {
  209.  
  210. return len;
  211. }
  212.  
  213. /*
  214. * (non-Javadoc)
  215. *
  216. * @see eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#sublist(int,
  217. * int)
  218. */
  219. public ILinkedList sublist(int fromIndex, int toIndex) {
  220.  
  221. if (len == 0 || fromIndex < 0 || fromIndex > len || toIndex < 0 || toIndex > len || fromIndex > toIndex) {
  222. throw new RuntimeException("sublist error");
  223. }
  224. else {
  225. SinglyLinkedList sub = new SinglyLinkedList();
  226. int counter = fromIndex;
  227. while (counter <= toIndex) {
  228. sub.add(counter - fromIndex, get(counter));
  229. counter++;
  230. }
  231. return sub;
  232. }
  233. }
  234.  
  235. /*
  236. * (non-Javadoc)
  237. *
  238. * @see
  239. * eg.edu.alexu.csd.datastructure.linkedList.ILinkedList#contains(java.lang.
  240. * Object)
  241. */
  242. public boolean contains(Object o) {
  243.  
  244. if (len == 0) { // The list is empty
  245. return false;
  246. }
  247. else {
  248. Node link = new Node(null, head.next, null);
  249. while (link.next != null) { // Checking till the end of the list
  250. if ((link.next).data.equals(o)) { // or catching the element
  251. // whichever comes first
  252. return true;
  253. }
  254. (link.next).prev = link;
  255. link = link.next;
  256. }
  257. return false;
  258.  
  259. }
  260. }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement