Haytham_Shalabi

SingleLinkedList

Jan 12th, 2015
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. /**
  2. *
  3. * @author haytham
  4. */
  5. public class SingleLinkedList<E> {
  6. private Node<E> head;
  7. private Node<E> tail;
  8. private int size=0;
  9. public SingleLinkedList(){}
  10. public SingleLinkedList(E e){
  11. add(e);
  12. }
  13. public SingleLinkedList(E[] e){
  14. for(E x:e)
  15. add(x);
  16. }
  17. public void add(E e){
  18. if(head==null){
  19. head=tail=new Node<E>(e);
  20. size++;
  21. }
  22. else {
  23. Node<E> node=new Node<E>(e);
  24. tail.next=node;
  25. tail=tail.next;
  26. size++;
  27. }}
  28. public void add(int index,E e){
  29. if(index<0||index>=size){
  30. return ;
  31. }
  32. Node<E> node=new Node<E>(e);
  33. Node<E> current=head;
  34. for(int i=0;i<index-2;i++){
  35. current=current.next;
  36. }
  37. node.next=current.next;
  38. current.next=node;
  39. size++;
  40. }
  41. public void addFirst(E e){
  42. if(isEmpty()){
  43. head=tail=new Node<E>(e);
  44. size++;
  45. }
  46. else{
  47. Node<E> node=new Node<E>(e);
  48. node.next=head;
  49. head=node;
  50. size++;
  51. }
  52. }
  53. public void addAll(E[] arr){
  54. for(E x:arr){
  55. add(x);
  56. }
  57. }
  58. public void clear(){
  59. head=null;
  60. size=0;
  61. }
  62. public boolean contains(E e){
  63. Node<E> current=head;
  64. while(current!=null){
  65. if(current.data.equals(e))
  66. return true;
  67. current=current.next;
  68. }
  69. return false;
  70. }
  71. public E get(int index){
  72. if(size()==0)
  73. return null;
  74.  
  75. Node<E> current=head;
  76. for(int i=0;i<index;i++){
  77. current=current.next;
  78. }
  79. return current.data;
  80. }
  81. public boolean isEmpty(){
  82. return size()==0;
  83. }
  84. public int size(){
  85. return size;
  86. }
  87. public E getFirst(){
  88. return head.data;
  89. }
  90. public E getLast(){
  91. return tail.data;
  92. }
  93. public int indexOf(E e){
  94. if(!contains(e)||isEmpty())
  95. return -1;
  96. Node<E> current=head;
  97. for(int i=0;i<size();i++){
  98. if(get(i).equals(e))
  99. return i;
  100. }
  101. return -1;
  102. }
  103. public int lastIndexOf(E e){
  104. Node<E> current=head;
  105. int index=-1;
  106. int i=0;
  107. while(current!=null){
  108. if(current.data.equals(e)){
  109. index=i;
  110. current=current.next;
  111. } else {
  112. current=current.next;
  113. }
  114. i++;
  115. }
  116. return index;
  117. }
  118. public E removeFirst(){
  119. if(size()==0)
  120. return null;
  121. Node<E> node=head;
  122. head=head.next;
  123. size--;
  124. return node.data;
  125. }
  126. public E removeLast(){
  127. if(size()==0)
  128. return null;
  129. if(size()==1){
  130. head=tail=null;
  131. size--;
  132. }
  133. Node<E> node=tail;
  134. Node<E> current=head;
  135. while(current.next.next!=null){
  136. current=current.next;
  137. }
  138. tail=null;
  139. tail=current;
  140. size--;
  141. return node.data;
  142. }
  143. public boolean remove(E e){
  144. if(contains(e)){
  145. remove(indexOf(e));
  146. return true;
  147. }
  148. return false;
  149. }
  150. public E remove(int index){
  151. if(index>=size()||index<0)
  152. return null;
  153. if(index==0)
  154. return removeFirst();
  155. if(index==size()-1)
  156. return removeLast();
  157. Node<E> element;
  158. Node<E> current=head;
  159. for(int i=0;i<index-2;i++){
  160. current=current.next;
  161. }
  162. element=current.next;
  163. current.next=element.next;
  164. size--;
  165. return element.data;
  166. }
  167. public void reverse(){
  168. reverse(head);
  169. }
  170.  
  171. private void reverse(Node<E> node){
  172. if(node.next==null){
  173. head=node;
  174. return ;
  175. }
  176. reverse(node.next);
  177. node.next.next=node;
  178. node.next=null;
  179. }
  180. public void MoveLastToHead(){
  181. if(head==null||size()==1){
  182. return;
  183. }
  184. else {
  185. addFirst(removeLast());
  186. }
  187. }
  188. public void MoveLastToFirst(){
  189. Node<E> last=tail;
  190. Node<E> current=head;
  191. while(current.next.next!=null){
  192. current=current.next;
  193. }
  194. tail=null;
  195. tail=current;
  196. last.next=head;
  197. head=last;
  198. }
  199.  
  200. public void reverse2(){
  201. Node<E> current=head;
  202. Node<E> prev=null;
  203. Node<E> next;
  204. while(current!=null){
  205. next=current.next;
  206. current.next=prev;
  207. prev=current;
  208. current=next;
  209. }
  210. head=prev;
  211.  
  212. }
  213. public void RotateLeft(int times){
  214. for(int i=0;i<times;i++){
  215. RotateLeft();
  216. }
  217. }
  218. private void RotateLeft(){
  219. if(size()==0||size()==1)
  220. return ;
  221. Node<E> node=tail;
  222. Node<E> current=head;
  223. while(current.next.next!=null){
  224. current=current.next;
  225. }
  226. current.next=null;
  227. tail=current;
  228. node.next=head;
  229. head=node;
  230. }
  231. public void RotateRgiht(int times){
  232. for(int i=0;i<times;i++){
  233. RotateRight();
  234. }
  235. }
  236. public void RotateRight(){
  237. if(size()==0||size()==1)
  238. return ;
  239. Node<E> node=head;
  240. head=head.next;
  241. tail.next=node;
  242. tail=node;
  243. }
  244. public static void main(String[] args) {
  245. SingleLinkedList<Integer> list=new SingleLinkedList<Integer>();
  246. list.add(1);
  247. list.add(2);
  248. list.add(3);
  249. list.add(4);
  250. for(int i=0;i<list.size();i++){
  251. System.out.print(list.get(i)+" ");
  252. }
  253.  
  254. System.out.println();
  255. list.RotateRgiht(1);
  256. for(int i=0;i<list.size();i++){
  257. System.out.print(list.get(i)+" ");
  258. }
Advertisement
Add Comment
Please, Sign In to add comment