Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. /**
  2. * In-lab assignment: Threading 1. Create 2 threads to add the data provided in
  3. * main <1 mark> 2. After the threads have FINISHED print the resulting linked
  4. * list (in-order) <1 mark> 3. Exacerbate any existing race conditions <1 mark>
  5. *
  6. *
  7. ********************************************************************************************/
  8.  
  9. interface DIterator<T> {
  10. boolean isEmpty();
  11.  
  12. boolean hasNext();
  13.  
  14. boolean hasPrevious();
  15.  
  16. T next();
  17.  
  18. T previous();
  19. }
  20.  
  21. class DLinkedList<T extends Comparable<T>> {
  22. private DNode head;
  23.  
  24. private class DNode {
  25. T data;
  26. DNode previous, next;
  27.  
  28. DNode(T d) {
  29. data = d;
  30. }
  31. }
  32.  
  33. public void clear() {
  34. head = null;
  35. }
  36.  
  37. public boolean insert(T d) {
  38. try {
  39. DNode temp = new DNode(d);
  40. DNode cur = head;
  41. DNode prev = head;
  42. // 1. empty list case
  43. if (head == null) {// check if list is empty
  44. head = temp;// if empty, put new item at the start of the list
  45. return true;
  46. }
  47. // 2. non-empty list, find position
  48. while ((cur.next != null) && (cur.data.compareTo(d) < 0)) {
  49. prev = cur;
  50. cur = cur.next;
  51. }
  52. // 3. value exists in list already - fail
  53. if (cur.data.compareTo(d) == 0)
  54. return false;
  55. // 4. single node in list
  56. if (cur == prev) {
  57. // 5. single node < new node
  58. if (cur.data.compareTo(d) < 0) {
  59. cur.next = temp;
  60. temp.previous = cur;
  61. return true;
  62. }
  63. // 6. single node > new node
  64. temp.next = cur;
  65. cur.previous = temp;
  66. head = temp;
  67. return true;
  68. }
  69. // 7. multiple nodes in list
  70. prev.next = temp;
  71. temp.next = cur;
  72. // 8. check if being added at the start of the list
  73. // if it is there is no previous node and the head of list
  74. // needs to change
  75. if (cur.previous != null)
  76. cur.previous = temp;
  77. else
  78. head = temp;
  79. temp.previous = prev;
  80. } catch (Exception e) {
  81. }
  82. return true;
  83. }
  84.  
  85. public DIterator<T> iterator() {
  86. return new DIterator<T>() {
  87. DNode cur = head;
  88.  
  89. public boolean isEmpty() {
  90. if (cur != null)
  91. return false;
  92. return true;
  93. }
  94.  
  95. public boolean hasNext() {
  96. return cur.next != null;
  97. }
  98.  
  99. public boolean hasPrevious() {
  100. return cur.previous != null;
  101. }
  102.  
  103. public T next() {
  104. T d = cur.data;
  105. cur = cur.next;
  106. return d;
  107. }
  108.  
  109. public T previous() {
  110. T d = cur.data;
  111. cur = cur.previous;
  112. return d;
  113. }
  114. };
  115. }
  116. }
  117.  
  118. // CREATE YOUR RUNNABLE CLASS(ES) HERE FOR THREADING
  119.  
  120. public class RaceTest {
  121. static DLinkedList<Integer> list = new DLinkedList<Integer>();
  122.  
  123. public static void main(String[] args) {
  124. // int[] prime1 = {47,13,23,17};//for Thread1
  125. // int[] prime2 = {5,19,37,7};//for Thread2
  126.  
  127. // make threads and launch them
  128.  
  129. // make sure you WAIT for Thread1 and Thread2 to complete before attempting to
  130. // print
  131. print(list);// result should display missing data, data out of order, different each time
  132. }
  133.  
  134. public static <P extends Comparable<P>> void print(DLinkedList<P> list) {
  135. DIterator<P> i = list.iterator();
  136. while (!i.isEmpty())
  137. System.out.print("" + i.next() + " ");
  138. System.out.println("");
  139. }
  140.  
  141. public static <P extends Comparable<P>> void printR(DLinkedList<P> list) {
  142. DIterator<P> i = list.iterator();
  143. while (i.hasNext())
  144. i.next();
  145. while (!i.isEmpty())
  146. System.out.print("" + i.previous() + " ");
  147. System.out.println("");
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement