Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1.  
  2.  
  3.  
  4. public class ILinkedList {
  5.  
  6. private Node head ;
  7. private Node tail ;
  8. private int size ;
  9.  
  10. public Node getHead() {
  11. return head;
  12. }
  13. public void setHead(Node head) {
  14. this.head = head;
  15. }
  16. public Node getTail() {
  17. return tail;
  18. }
  19. public void setTail(Node tail) {
  20. this.tail = tail;
  21. }
  22. public int getSize() {
  23. return size;
  24. }
  25. public void setSize(int size) {
  26. this.size = size;
  27. }
  28.  
  29. public void add (int index , Term a)
  30. {
  31. Node n = new Node (a);
  32. Node curr = head ;
  33. if( index > size - 1 || index < 0)
  34. {
  35. return;
  36. }
  37. if ( head == null )
  38. {
  39. head = n ;
  40. tail = n ;
  41. }
  42. else if (index == 0)
  43. {
  44.  
  45. n.setNext(head);
  46. head = n;
  47. }
  48. else if (index == size )
  49. {
  50. add( a );
  51. }
  52. else
  53. {
  54. for( int i = 0 ; i < index - 1 ; i++ )
  55. {
  56. curr = curr.getNext();
  57. }
  58.  
  59. n.setNext(curr.getNext());
  60.  
  61. curr.setNext(n);
  62. }
  63. size++;
  64. }
  65. public void add(Term a)
  66. {
  67. Node n = new Node (a) ;
  68. if( head == null )
  69. {
  70. head = n ;
  71. }
  72. else
  73. {
  74. tail.setNext(n);
  75.  
  76. }
  77. tail = n ;
  78. size++ ;
  79. }
  80. public Term get ( int index )
  81. {
  82. if( index < 0 || index > size - 1 )
  83. {
  84. return null ;
  85. }
  86. Node curr = head ;
  87. if(head == null )
  88. {
  89. return null ;
  90. }
  91. for(int i = 0 ; i < index ; i++ )
  92. {
  93. curr = curr.getNext();
  94. }
  95. return curr.getX();
  96. }
  97. public void set( int index , Term a )
  98. {
  99. if( index > size - 1 || index < 0)
  100. {
  101. return;
  102. }
  103. Node curr = head ;
  104. for(int i = 0 ; i < index ; i++ )
  105. {
  106. curr = curr.getNext();
  107. }
  108. curr.setX(a);
  109. }
  110. public void clear ( )
  111. {
  112. head = null ;
  113. tail = null ;
  114. size = 0 ;
  115. }
  116. public boolean isEmpty ()
  117. {
  118. return head == null ;
  119. }
  120. public void remove (int index)
  121. {
  122. if( head == null || index > size - 1 || index < 0 )
  123. {
  124. return;
  125. }
  126. if( head == tail )
  127. {
  128. head = null ;
  129. tail = null ;
  130. }
  131. else if( index == 0 )
  132. {
  133. Node removed = head ;
  134. head = head.getNext();
  135. removed.setNext(null);
  136. }
  137. else
  138. {
  139. Node curr = head;
  140. for(int i = 0 ; i < index-1 ; i++ )
  141. {
  142. curr = curr.getNext();
  143. }
  144. Node removed=curr.getNext();
  145. curr.setNext(removed.getNext());
  146. removed.setNext(null);
  147. }
  148. size--;
  149. }
  150. public ILinkedList sublist ( int fromIndex , int toIndex )
  151. {
  152. if ( head == null || fromIndex < 0 || fromIndex > size - 1 || toIndex < 0 || toIndex > size - 1 || fromIndex > toIndex)
  153. {
  154. return null ;
  155. }
  156. int sizeSublist = ( toIndex - fromIndex ) + 1 ;
  157. ILinkedList sublist = new ILinkedList() ;
  158. Node curr = head ;
  159. for( int i = 0 ; i < size ; i++)
  160. {
  161. if( IsIncluded(i, fromIndex, toIndex) )
  162. {
  163. sublist.add(curr.getX());
  164. }
  165. curr = curr.getNext() ;
  166. }
  167. sublist.setSize(sizeSublist);
  168. return sublist ;
  169. }
  170. public boolean IsIncluded (int index , int fromIndex , int toIndex)
  171. {
  172. return index >= fromIndex && index <= toIndex ;
  173. }
  174. public boolean contains ( Term a )
  175. {
  176. if ( head == null )
  177. {
  178. return false ;
  179. }
  180. Node curr = head ;
  181. while (curr != null )
  182. {
  183. if( curr.getX().equals(a) )
  184. {
  185. return true;
  186. }
  187. curr = curr.getNext() ;
  188. }
  189. return false ;
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement