fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. class list<T>
  5. {
  6. node head;
  7. node tail;
  8. public int size;
  9. class node
  10. {
  11. T data;
  12. node next;
  13. node(T d)
  14. {
  15. data=d;
  16. next=null;
  17. }
  18. }
  19.  
  20. list()
  21. {
  22. head=null;
  23. tail=null;
  24. size=0;
  25. }
  26.  
  27. public void add_head(T d)
  28. {
  29. node newnode=new node(d);
  30. if(head==null)
  31. {
  32. head=newnode;
  33. tail=newnode;
  34. }
  35. else
  36. {
  37. newnode.next=head;
  38. head=newnode;
  39. }
  40. size++;
  41. }
  42.  
  43. public void add_tail(T d)
  44. {
  45. node newnode=new node(d);
  46. if(head==null)
  47. {
  48. head=newnode;
  49. tail=newnode;
  50. }
  51. else
  52. {
  53. tail.next=newnode;
  54. tail=newnode;
  55. }
  56. size++;
  57. }
  58.  
  59.  
  60. public void add(int p,T d)
  61. {
  62.  
  63. if(p<0 || p>size)
  64. {
  65. System.out.println("wrong position");
  66. return;
  67. }
  68. if (head==null || p==0)
  69. add_head(d);
  70. else if(p==size)
  71. add_tail(d);
  72. else
  73. {
  74. node newnode=new node(d);
  75. node pred=head;
  76. node succ=head;
  77. while(p>0)
  78. {
  79. pred=succ;
  80. succ=succ.next;
  81. p--;
  82. }
  83. pred.next=newnode;
  84. newnode.next=succ;
  85. size++;
  86. }
  87. }
  88.  
  89. void del_head()
  90. {
  91. node t;
  92. if (head==null)
  93. {
  94. System.out.println("list is empty");
  95. return;
  96. }
  97. if(tail==head)
  98. {
  99. head=tail=null;
  100. size=0;
  101. }
  102. else
  103. {
  104. t=head;
  105. head=head.next;
  106. t.next=null;
  107. t=null;
  108. size--;
  109. }
  110. }
  111. void del_tail()
  112. {
  113. node t;
  114. if (head==null)
  115. {
  116. System.out.println("list is empty");
  117. return;
  118. }
  119. if(tail==head)
  120. {
  121. head=tail=null;
  122. size=0;
  123. }
  124. else
  125. {
  126. node pred,succ;
  127. pred=succ=head;
  128. while(succ.next!=null)
  129. {
  130. pred=succ;
  131. succ=succ.next;
  132. }
  133. pred.next=null;
  134. tail=pred;
  135. succ=null;
  136. size--;
  137.  
  138. }
  139. }
  140. public void del(int p)
  141. {
  142.  
  143. if(p<0 || p>size-1)
  144. {
  145. System.out.println("wrong position");
  146. return;
  147. }
  148. if (head==null)
  149. {
  150. System.out.println("list is empty");
  151. return;
  152. }
  153. else if (p==0)
  154. del_head();
  155. else if(p==size-1)
  156. del_tail();
  157. else
  158. {
  159. node pred=head;
  160. node succ=head;
  161. while(p>0)
  162. {
  163. pred=succ;
  164. succ=succ.next;
  165. p--;
  166. }
  167. pred.next=succ.next;
  168. succ.next=null;
  169. succ=null;
  170. size--;
  171. }
  172. }
  173. void print()
  174. {
  175. node n=head;
  176. while(n!=null)
  177. {
  178. System.out.print(n.data+" ");
  179. n=n.next;
  180. }
  181. System.out.println();
  182. }
  183. int getSize()
  184. {
  185. return size;
  186. }
  187.  
  188. String max()
  189. {
  190. try
  191. {
  192. node n=head;
  193. node m=head;
  194. while(n!=null)
  195. {
  196. if( (int) n.data> (int ) m.data) m=n;
  197. n=n.next;
  198. }
  199. return Integer.toString((int ) m.data);
  200. } catch(Exception e){return "list is empty";}
  201. }
  202. boolean search(T d)
  203. {
  204. try
  205. {
  206. node n=head;
  207. while(n!=null)
  208. {
  209. if(n.data==d) return true;
  210. n=n.next;
  211. }
  212. return false;
  213. }catch(Exception e){return false;}
  214. }
  215. }
  216.  
  217. class Ideone
  218. {
  219. public static void main(String argv[])
  220. {
  221. list<Integer> l=new list<Integer>();
  222. l.add_head(1);
  223. l.add_head(2);
  224. l.add_tail(3);
  225. l.add_tail(4);
  226. l.add(2,5);
  227. l.print();
  228. System.out.println(l.search(6));
  229. System.out.println(l.max());
  230. l.del_head();
  231. l.print();
  232. l.del_tail();
  233. l.print();
  234. l.del(2);
  235. l.print();
  236. System.out.println(l.getSize());
  237. }
  238. }
  239.  
Success #stdin #stdout 0.15s 52284KB
stdin
Standard input is empty
stdout
2 1 5 3 4 
false
5
1 5 3 4 
1 5 3 
1 5 
2