Guest User

Untitled

a guest
Nov 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Numbers {
  4.  
  5. /**
  6. * @param args
  7. */
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. Scanner reader = new Scanner (System.in);
  11. LinkedList link=new LinkedList();
  12. LinkedList link2= new LinkedList();
  13. System.out.println("Enter in 5 numbers to put in your list");
  14. int num1, num2, num3, num4, num5;
  15. num1 = reader.nextInt();
  16. link.addToStart(num1);
  17. num2 = reader.nextInt();
  18. link.addToStart(num2);
  19. num3 = reader.nextInt();
  20. link.addToStart(num3);
  21. num4 = reader.nextInt();
  22. link.addToStart(num4);
  23. num5 = reader.nextInt();
  24. link.addToStart(num5);
  25.  
  26. link2.addToStart(num5);
  27. link2.addToStart(num4);
  28. link2.addToStart(num3);
  29. link2.addToStart(num2);
  30. link2.addToStart(num1);
  31.  
  32. System.out.println("The size of the linked list is " + link.size());
  33.  
  34. System.out.print("Here is the list ");
  35. link2.outputList();
  36. System.out.println();
  37. System.out.print("Here is the list in reverse order ");
  38. link.outputList( );
  39. System.out.println();
  40.  
  41. if (num1==num2){
  42. link2.deleteHeadNode(num1);
  43. link2.deleteHeadNode(num2);
  44. System.out.println("Here is the list with the removed numbers");
  45. link2.outputList();
  46. System.out.println();
  47. System.out.println("Here is its size");
  48. System.out.println(link2.size());
  49. }
  50. else if (num2==num3){
  51. link2.deleteHeadNode(num2);
  52. link2.deleteHeadNode(num3);
  53. System.out.println("Here is the list with the removed numbers");
  54. link2.outputList();
  55. System.out.println();
  56. System.out.println("Here is its size");
  57. System.out.println(link2.size());
  58. }
  59. }
  60.  
  61. }
  62.  
  63. public class Node1
  64. {
  65. private Object item;
  66. private int count;
  67. private Node1 link;
  68.  
  69. public Node1( )
  70. {
  71. link = null;
  72. item = null;
  73. count = 0;
  74. }
  75.  
  76. public Node1(int num, int newCount, Node1 linkValue)
  77. {
  78. setData(num, newCount);
  79. link = linkValue;
  80. }
  81.  
  82. public void setData(int num, int newCount)
  83. {
  84. item = num;
  85. count = newCount;
  86. }
  87.  
  88. public void setLink(Node1 newLink)
  89. {
  90. link = newLink;
  91. }
  92.  
  93. public Object getItem( )
  94. {
  95. return item;
  96. }
  97.  
  98. public int getCount( )
  99. {
  100. return count;
  101. }
  102.  
  103. public Node1 getLink( )
  104. {
  105. return link;
  106. }
  107. }
  108.  
  109. public class LinkedList
  110. {
  111. private Node1 head;
  112.  
  113. public LinkedList( )
  114. {
  115. head = null;
  116. }
  117.  
  118. /**
  119. Adds a node at the start of the list with the specified data.
  120. The added node will be the first node in the list.
  121. */
  122. public void addToStart(int num)
  123. {
  124. head = new Node1(num, num, head);
  125. }
  126.  
  127. /**
  128. Removes the head node and returns true if the list contains at least
  129. one node. Returns false if the list is empty.
  130. * @param num1
  131. */
  132. public boolean deleteHeadNode(int num1 )
  133. {
  134. if (head != null)
  135. {
  136. head = head.getLink( );
  137. return true;
  138. }
  139. else
  140. return false;
  141. }
  142.  
  143. /**
  144. Returns the number of nodes in the list.
  145. */
  146. public int size( )
  147. {
  148. int count = 0;
  149. Node1 position = head;
  150.  
  151. while (position != null)
  152. {
  153. count++;
  154. position = position.getLink( );
  155. }
  156. return count;
  157. }
  158.  
  159. public boolean contains(String item)
  160. {
  161. return (find(item) != null);
  162. }
  163.  
  164. /**
  165. Finds the first node containing the target item, and returns a
  166. reference to that node. If target is not in the list, null is returned.
  167. */
  168. private Node1 find(String target)
  169. {
  170. Node1 position = head;
  171. Object itemAtPosition;
  172. while (position != null)
  173. {
  174. itemAtPosition = position.getItem( );
  175. if (itemAtPosition.equals(target))
  176. return position;
  177. position = position.getLink( );
  178. }
  179. return null; //target was not found
  180. }
  181.  
  182. public void outputList( )
  183. {
  184. Node1 position = head;
  185. while (position != null)
  186. {
  187. System.out.print(position.getItem( ) + " ");
  188. position = position.getLink( );
  189. }
  190. }
  191.  
  192. public boolean isEmpty( )
  193. {
  194. return (head == null);
  195. }
  196.  
  197. public void clear( )
  198. {
  199. head = null;
  200. }
  201.  
  202. }
  203.  
  204. public void removeNode(Node previousNode, Node nodeToRemove) {
  205. if (previousNode != null) {
  206. previousNode.setLink(nodeToRemove.getLink());
  207. }
  208. }
  209.  
  210. N1 -> N2 -> N3 -> N4
  211.  
  212. N1 -> N3 -> N4
  213.  
  214. forEach( Element a : inList ) do
  215. // e is the element we want to find repeated.
  216. forEach( Element b : inList ) do
  217. // b is the element in the list.
  218. if( a == b ) then // repeated
  219. inList.remove( a )
  220. break;
  221. endIf
  222. endFor
  223. endFor
  224.  
  225. n1 -> n2 -> n3
  226.  
  227. n1 -> n3 n2 ->n3
  228.  
  229. n1 -> n3
  230.  
  231. public void delete(int item)
  232. {
  233. while(head.data==item) //For deleting head
  234. {
  235.  
  236. head=head.link;
  237.  
  238. }
  239. // For middle elements..................
  240. Node ptr, save;
  241. save=head;
  242. ptr=head.link;
  243. while(ptr!=null)
  244. {
  245. if(ptr.data==item)
  246. {
  247. Node next=ptr.link;
  248. save.link=next;
  249. ptr=next;
  250. }
  251. else
  252. {
  253. save=ptr;
  254. ptr=ptr.link;
  255. }
  256.  
  257. }
  258. }
Add Comment
Please, Sign In to add comment