Guest User

Untitled

a guest
Feb 25th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. /*
  2. * Name = Abdulmohsen ali asiri
  3. * ID = 36110218
  4. */
  5. package testintegerdll;
  6.  
  7. public class DLL<T> {
  8.  
  9. private DLLNode<T> head, tail;
  10.  
  11. public DLL() {
  12. head = tail = null;
  13. }
  14.  
  15. public boolean isEmpty() {
  16. return head == null;
  17. }
  18.  
  19. public void clear() {
  20. head = tail = null;
  21. }
  22.  
  23. public T getFirst() {
  24. if (head != null) {
  25. return head.info;
  26. } else {
  27. return null;
  28. }
  29. }
  30.  
  31. public void addToHead(T el) {
  32. if (head != null) {
  33. head = new DLLNode<T>(el, head, null);
  34. head.next.prev = head;
  35. } else {
  36. head = tail = new DLLNode<T>(el);
  37. }
  38. }
  39.  
  40. public void addToTail(T el) {
  41. if (tail != null) {
  42. tail = new DLLNode<T>(el, null, tail);
  43. tail.prev.next = tail;
  44. } else {
  45. head = tail = new DLLNode<T>(el);
  46. }
  47. }
  48.  
  49. public T deleteFromHead() {
  50. if (isEmpty()) {
  51. return null;
  52. }
  53. T el = head.info;
  54. if (head == tail) // if only one node on the list;
  55. {
  56. head = tail = null;
  57. } else { // if more than one node in the list;
  58. head = head.next;
  59. head.prev = null;
  60. }
  61. return el;
  62. }
  63.  
  64. public T deleteFromTail() {
  65. if (isEmpty()) {
  66. return null;
  67. }
  68. T el = tail.info;
  69. if (head == tail) // if only one node on the list;
  70. {
  71. head = tail = null;
  72. } else { // if more than one node in the list;
  73. tail = tail.prev;
  74. tail.next = null;
  75. }
  76. return el;
  77. }
  78.  
  79. public void delete(T el) { // delete the node with an element el;
  80. DLLNode<T> tmp;
  81. for (tmp = head; tmp != null && !el.equals(tmp.info); tmp = tmp.next); //locate the item
  82.  
  83. if (tmp != null) { // item found
  84. if (head == tail) //the found item was the only one
  85. {
  86. head = tail = null;
  87. } else if (head == tmp) { //the found item is the first
  88. head = head.next;
  89. head.prev = null;
  90. } else if (tail == tmp) { // the found item is the last
  91. tail = tail.prev;
  92. tail.next = null;
  93. } else { // the found item is in the middle
  94. tmp.prev.next = tmp.next;
  95. tmp.next.prev = tmp.prev;
  96. }
  97. }
  98. }
  99.  
  100. public void printAll() {
  101. for (DLLNode<T> tmp = head; tmp != null; tmp = tmp.next) {
  102. System.out.print(tmp.info + " ");
  103. }
  104. }
  105.  
  106. public T find(T el) {
  107. DLLNode<T> tmp;
  108. for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
  109. if (tmp == null) {
  110. return null;
  111. } else {
  112. return tmp.info;
  113. }
  114. }
  115.  
  116. // node of generic doubly linked list class
  117. class DLLNode<T> {
  118.  
  119. public T info;
  120. public DLLNode<T> next, prev;
  121.  
  122. public DLLNode() {
  123. next = null;
  124. prev = null;
  125. }
  126.  
  127. public DLLNode(T el) {
  128. info = el;
  129. next = null;
  130. prev = null;
  131. }
  132.  
  133. public DLLNode(T el, DLLNode<T> n, DLLNode<T> p) {
  134. info = el;
  135. next = n;
  136. prev = p;
  137. }
  138. }
  139.  
  140. public int length() {
  141. int count = 0;
  142. DLLNode<T> tmp = head;
  143. for (tmp = head; tmp != null; tmp = tmp.next, count++);
  144. return count;
  145. }
  146. //start of assignment
  147. public T get(int index) {
  148. DLLNode<T> tmp = head;
  149. int count = 0;
  150.  
  151. if (index < 0 || index > length() - 1) {
  152. return null;
  153. } else {
  154.  
  155. for (tmp = head; count != index; tmp = tmp.next, count++);
  156. return tmp.info;
  157. }
  158.  
  159. }
  160.  
  161. public int indexOf(T el) {
  162. DLLNode<T> tmp = head;
  163. int count = 0;
  164. for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next, count++);
  165. if (tmp == null) {
  166. return -1;
  167. } else {
  168. return count;
  169. }
  170. }
  171.  
  172. public void replace(int index, T el) {
  173. DLLNode<T> tmp = head;
  174. int count = 0;
  175.  
  176. for (tmp = head; tmp != null && count != index; tmp = tmp.next, count++);
  177. if (tmp != null) {
  178. tmp.info = el;
  179. }
  180. }
  181.  
  182. public void insertAt(int index, T el) {
  183. DLLNode<T> tmp = head;
  184. int count = 0;
  185.  
  186. if (index >= 0 && index <= length()) {
  187. if (index == 0) {
  188. addToHead(el);
  189. }
  190. else if (index == length()) {
  191. addToTail(el);
  192. }
  193. else {
  194. for (tmp = head; count != index; tmp = tmp.next, count++);
  195. tmp.prev.next = tmp.prev = new DLLNode<T>(el, tmp, tmp.prev);
  196. }
  197. }
  198.  
  199. }
  200.  
  201. public T deleteAt(int index) {
  202. DLLNode<T> tmp = head;
  203. int count = 0;
  204.  
  205. if (index < 0 || index > length() - 1) {
  206. return null;
  207. } else if (isEmpty()) {
  208. return null;
  209. } else if (length() == 1) {
  210. T el = head.info;
  211. clear();
  212. return el;
  213. } else if (index == 0) {
  214. T el = tmp.info;
  215. head = tmp.next;
  216. head.prev = null;
  217. return el;
  218. } else if (index == length() - 1) {
  219. T el = tail.info;
  220. tail = tail.prev;
  221. tail.next = null;
  222. return el;
  223. } else {
  224. for (tmp = head; count != index; tmp = tmp.next, count++);
  225. T el = tmp.info;
  226. tmp.next.prev = tmp.prev;
  227. tmp.prev.next = tmp.next;
  228. return el;
  229. }
  230. }
  231.  
  232. }
Add Comment
Please, Sign In to add comment