Advertisement
yassmin

Untitled

May 9th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.linkedList.cs89;
  2.  
  3. import eg.edu.alexu.csd.datastructure.linkedList.ILinkedList;
  4.  
  5. public class LinkedlistDoubly implements ILinkedList{
  6.  
  7. public Link head = null;
  8. public int size=0;
  9. @Override
  10. public void add(int index, Object element) {
  11.  
  12.  
  13.  
  14. if(index < 0 || index > size || element.equals(null)) throw new RuntimeException("unfound 1");
  15. Link NOW = head ;
  16. int count=0;
  17. while(count < index-1 ){
  18. NOW = NOW.next;
  19. count++;
  20. }
  21.  
  22.  
  23.  
  24. if( index ==0 ){
  25. if(head == null){
  26. Link addone = new Link(null, element ,null);
  27. head = addone ;
  28. }
  29. else{
  30. Link addone = new Link(null, element ,NOW);
  31. head = addone ;
  32. }
  33.  
  34. }
  35. else{
  36. Link addone = new Link(NOW, element ,NOW.next);
  37. NOW.next = addone ;
  38. }
  39. size++;
  40. // TODO Auto-generated method stub
  41.  
  42. }
  43.  
  44. @Override
  45. public void add(Object element) {
  46. if (element.equals(null))throw new RuntimeException("unfound 1");
  47. else if( head == null){
  48. Link end = new Link (null,element ,null);
  49. head= end;
  50. }
  51. else{
  52. Link NOW = head ;
  53. for(int counter =0 ; NOW.next != null ; counter++){
  54. NOW= NOW.next;
  55. }
  56. Link end = new Link (NOW,element ,null);
  57. NOW.next= end;
  58. }
  59. size++;
  60. // TODO Auto-generated method stub
  61.  
  62. }
  63.  
  64. @Override
  65. public Object get(int index) {
  66. if(index<0 || index >= size) throw new RuntimeException("unfound 2");
  67.  
  68. if( head == null) throw new RuntimeException("unfound 3");
  69. Link NOW = head ;
  70. for(int counter =0 ; counter < index ; counter++){
  71. NOW= NOW.next;
  72. }
  73. return NOW.data;
  74. // TODO Auto-generated method stub
  75.  
  76. }
  77.  
  78. @Override
  79. public void set(int index, Object element) {
  80. if(index<0 || index >= size || element.equals(null) ) throw new RuntimeException("unfound 4 ") ;
  81.  
  82. if( head == null) throw new RuntimeException("unfound 5 ") ;
  83. Link NOW = head ;
  84. for(int counter =0 ; counter < index ; counter++){
  85. NOW= NOW.next;
  86. }
  87. NOW.data = element;
  88. // TODO Auto-generated method stub
  89.  
  90. }
  91.  
  92. @Override
  93. public void clear() {
  94. if(head != null){
  95. head=null;
  96. }
  97.  
  98. size=0;
  99.  
  100. // TODO Auto-generated method stub
  101.  
  102. }
  103.  
  104. @Override
  105. public boolean isEmpty() {
  106. if (head==null) return true;
  107.  
  108. // TODO Auto-generated method stub
  109. return false;
  110. }
  111.  
  112. @Override
  113. public void remove(int index) {
  114. if(index < 0 || index >= size) throw new RuntimeException("unfound 6");;
  115.  
  116. if( head == null || size < 0) throw new RuntimeException("unfound 7") ;
  117. Link NOW = head ;
  118. if(index == 0){
  119. head = NOW.next;
  120. NOW.next = null;
  121. head.previous = null;
  122. }
  123. else{
  124. for(int count = 1; count < index; count++){
  125. NOW = NOW.next;
  126. }
  127. Link second = new Link();
  128. Link third = new Link();
  129. second = NOW.next;
  130. if(second.next == null){
  131. NOW.next = null;
  132. second.previous = null;
  133. }
  134. else{
  135. NOW.next = second.next;
  136. third = second.next;
  137. third .previous = NOW;
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /*
  146. for(int counter =0 ; counter < index ; counter++){
  147. NOW= NOW.next;
  148. }
  149. if (size == 1 && index == 0){
  150. head = null;
  151. }
  152. else if (index == 0 && NOW.next != null ){
  153. head = NOW.next;
  154. NOW.next.previous=null;
  155. }
  156. else if (NOW.next== null){
  157. NOW.previous.next= null;
  158.  
  159. }
  160. else{
  161. /*NOW.next = NOW.next.next;
  162. NOW.next.next = null;
  163. NOW.next.previous = null;
  164. NOW.next.next.previous = NOW;
  165. */
  166.  
  167. /*NOW.previous.next= NOW.next;
  168. NOW.next.previous= NOW.previous;*/
  169. size--;
  170. // TODO Auto-generated method stub
  171. }
  172. @Override
  173. public int size() {
  174.  
  175. //if( head==null) return 0;
  176. /*Link NOW = head ;
  177. for(int counter =0 ; NOW.next != null ; counter++){
  178. size++;
  179. NOW= NOW.next;
  180. }
  181.  
  182. // TODO Auto-generated method stub*/
  183. return size;
  184. }
  185.  
  186. @Override
  187. public ILinkedList sublist(int fromIndex, int toIndex) {
  188. if(fromIndex <0 || fromIndex>= size || (fromIndex > toIndex )) throw new RuntimeException("unfound 8");
  189. if(toIndex <0 || toIndex>= size) throw new RuntimeException("unfound 9");
  190. if( head == null) throw new RuntimeException("unfound 10");
  191. Link NOW = head ;
  192. for(int counter =0 ; counter <fromIndex ; counter++){
  193. NOW= NOW.next;
  194. }
  195. ILinkedList sub = new LinkedlistDoubly();
  196. for( int count = fromIndex ; count<= toIndex; count++ ){
  197. sub.add(NOW.data);
  198. NOW=NOW.next;
  199. }
  200.  
  201.  
  202. /*if( NOW.next != null && counter < toIndex)
  203. {
  204.  
  205. }*/
  206. // TODO Auto-generated method stub
  207. return sub;
  208. }
  209.  
  210. @Override
  211. public boolean contains(Object o) {
  212. Link NOW = head ;
  213. if( o.equals(null))throw new RuntimeException("unfound 1111");
  214. for(int counter =0 ; NOW != null ; counter++){
  215. if( NOW.data.equals( o)){
  216. return true;
  217. }
  218. NOW= NOW.next;
  219. }
  220. // TODO Auto-generated method stub
  221. return false;
  222. }
  223. public void print (){
  224.  
  225. Link NOW = head ;
  226. while( NOW != null ){
  227. System.out.println(NOW.data);
  228. NOW =NOW.next;
  229. }
  230. System.out.println("#"+size);
  231. }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement