Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. package AssignmentThree;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class SinglyLinkedList
  6. {
  7. public SinglyLinkedList()
  8. {
  9. mFront = null;
  10. }
  11.  
  12. public void appendFront(String word)
  13. {
  14. Node newNode = new Node(word);
  15. newNode.mNext = mFront;
  16. mFront = newNode;
  17. }
  18.  
  19. public void appendRear(String word)
  20. {
  21. Node newNode = new Node(word);
  22.  
  23. if(mFront == null) mFront = newNode;
  24. else
  25. {
  26. Node current = mFront;
  27. while(current.mNext != null) current = current.mNext;
  28.  
  29. current.mNext = newNode;
  30. }
  31. }
  32.  
  33. public void insertBefore(String marker, String word)
  34. {
  35. Node newNode = new Node(word);
  36.  
  37. Node previous = null, current = mFront;
  38.  
  39. while(current != null)
  40. {
  41. if(current.mWord.equals(marker))
  42. {
  43. if(previous == null) appendFront(word);
  44. else
  45. {
  46. newNode.mNext = previous.mNext;
  47. previous.mNext = newNode;
  48. }
  49. break;
  50. }
  51.  
  52. previous = current;
  53. current = current.mNext;
  54. }
  55. }
  56.  
  57. public void insertAfter(String marker, String word)
  58. {
  59. Node newNode = new Node(word);
  60.  
  61. Node current = mFront;
  62.  
  63. while(current != null)
  64. {
  65. if(current.mWord.equals(marker))
  66. {
  67. newNode.mNext = current.mNext;
  68. current.mNext = newNode;
  69. break;
  70. }
  71. current = current.mNext;
  72. }
  73. }
  74.  
  75. public boolean isPresent(String word)
  76. {
  77. Node current = mFront;
  78.  
  79. while(current != null)
  80. {
  81. if(current.mWord.equals(word)) return true;
  82. current = current.mNext;
  83. }
  84.  
  85. return false;
  86. }
  87.  
  88. public void remove(String word)
  89. {
  90. Node previous = null, current = mFront;
  91.  
  92. while(current != null)
  93. {
  94. if(current.mWord.equals(word))
  95. {
  96. if(current == mFront)
  97. {
  98. mFront = mFront.mNext;
  99. }
  100. else
  101. {
  102. previous.mNext = current.mNext;
  103. }
  104. break;
  105. }
  106. else
  107. {
  108. previous = current;
  109. current = current.mNext;
  110. }
  111. }
  112. }
  113.  
  114. public void print()
  115. {
  116. System.out.println("\nUnsorted List");
  117.  
  118. Node current = mFront;
  119. while(current != null)
  120. {
  121. System.out.print(current.mWord + " ");
  122. current = current.mNext;
  123. }
  124.  
  125. System.out.println();
  126. }
  127.  
  128. public void sortAndPrint()
  129. {
  130. System.out.println("\nSorted List");
  131.  
  132. int count = getCount();
  133. if(count == 0) return;
  134.  
  135. String[] words = new String[count];
  136. Node current = mFront;
  137. int index = 0;
  138.  
  139. while(current != null)
  140. {
  141. words[index++] = current.mWord;
  142. current = current.mNext;
  143. }
  144.  
  145. Arrays.sort(words);
  146.  
  147. for(String word : words) System.out.print(word + " ");
  148. System.out.println();
  149. }
  150.  
  151. private int getCount()
  152. {
  153. int count = 0;
  154. Node current = mFront;
  155.  
  156. while(current != null)
  157. {
  158. ++count;
  159. current =current.mNext;
  160. }
  161.  
  162. return count;
  163. }
  164.  
  165. private class Node
  166. {
  167. public Node(String word)
  168. {
  169. mWord = word;
  170. mNext = null;
  171. }
  172.  
  173. private String mWord;
  174. private Node mNext;
  175. }
  176.  
  177. private Node mFront;
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement