Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. /** A data structure to represent a Linked List of Integers.
  2. * Each IntList represents one node in the overall Linked List.
  3. *
  4. * @author James ZHang and John Lin
  5. */
  6.  
  7. public class IntList {
  8.  
  9. /** The integer stored by this node. */
  10. private int item;
  11. /** The next node in this IntList. */
  12. private IntList next;
  13.  
  14. /** Constructs an IntList storing ITEM and next node NEXT. */
  15. public IntList(int item, IntList next) {
  16. this.item = item;
  17. this.next = next;
  18. }
  19.  
  20. /** Constructs an IntList storing ITEM and no next node. */
  21. public IntList(int item) {
  22. this(item, null);
  23. }
  24.  
  25. /** Returns an IntList consisting of the elements in ITEMS.
  26. * IntList L = IntList.list(1, 2, 3);
  27. * System.out.println(L.toString()) // Prints (1 2 3) */
  28. public static IntList list(int... items) {
  29. /** Check for cases when we have no element given. */
  30. if (items.length == 0) {
  31. return null;
  32. }
  33. /** Create the first element. */
  34. IntList head = new IntList(items[0]);
  35. IntList last = head;
  36. /** Create rest of the list. */
  37. for (int i = 1; i < items.length; i++) {
  38. last.next = new IntList(items[i]);
  39. last = last.next;
  40. }
  41. return head;
  42. }
  43.  
  44. /** Returns the integer stored by this IntList. */
  45. public int item() {
  46. return item;
  47. }
  48.  
  49. /** Returns the next node stored by this IntList. */
  50. public IntList next() {
  51. return next;
  52. }
  53.  
  54. /**
  55. * Returns [position]th item in this list. Throws IllegalArgumentException
  56. * if index out of bounds.
  57. *
  58. * @param position, the position of element.
  59. * @return The element at [position]
  60. */
  61. public int get(int position) {
  62. IntList first = new IntList(item(),next());
  63. if (position<0){
  64. //If position < 0, invalid.
  65. throw new IllegalArgumentException("Sorry, the position is out of range!");
  66. }
  67. for (int i=0; i<position;i++){
  68. if(first.next()==null && i<position){
  69. //If we've reached the end and we still haven't reached position, invalid.
  70. throw new IllegalArgumentException("Sorry, the position is out of range!");
  71. }
  72. first=first.next();
  73. }
  74. return first.item();
  75. }
  76.  
  77. /**
  78. * Returns the size of the list.
  79. *
  80. * @return The size of the list.
  81. */
  82. public int size() {
  83. IntList first = new IntList(item(),next());
  84. int counter = 1;
  85. while(first.next()!=null){
  86. first=first.next();
  87. counter ++;
  88. }
  89. return counter;
  90. }
  91.  
  92. /**
  93. * Returns the string representation of the list. For the list (1, 2, 3),
  94. * returns "( 1 2 3 )".
  95. *
  96. * @return The String representation of the list.
  97. */
  98. public String toString() { //Non-Recursive, Iterative Method
  99. String stringList = "( ";
  100. IntList first = new IntList(item(),next());
  101. while(first!=null){
  102. stringList = stringList + Integer.toString(first.item()) + " ";
  103. first=first.next();
  104. }
  105. stringList = stringList +")";
  106. return stringList;
  107. }
  108.  
  109. /**
  110. * Returns whether this and the given list or object are equal.
  111. *
  112. * @param obj, another list (object)
  113. * @return Whether the two lists are equal.
  114. */
  115. public boolean equals(Object obj) {
  116. if (obj instanceof IntList){ // Is it an IntList? If not, then they don't equal.
  117. if (((IntList) obj).size()==size()) { //If so, are they the same size?
  118. IntList firstList = new IntList(item(),next());
  119. IntList secondList = new IntList(((IntList) obj).item(), ((IntList) obj).next());
  120. while (firstList!=null){
  121. if (firstList.item()!=secondList.item()){
  122. return false;
  123. }
  124. firstList = firstList.next();
  125. secondList = secondList.next();
  126. }
  127. } else{
  128. return false;
  129. }
  130. } else{
  131. return false;
  132. } //If all conditions are reached, then woohoo! It's true!
  133. return true;
  134. }
  135.  
  136. /**
  137. * Adds the given item at the end of the list.
  138. *
  139. * @param item, the int to be added.
  140. */
  141. public void add(int item) {
  142. IntList first = new IntList(item(),next());
  143. IntList toAdd = new IntList(item);
  144. if (first.next==null){
  145. this.next=toAdd;
  146. return;
  147. }
  148. while (first.next()!=null){//Let's traverse to the end of the list!
  149. first=first.next();
  150. }
  151. first.next = toAdd;
  152. }
  153.  
  154. /**
  155. * Returns the smallest element in the list.
  156. *
  157. * @return smallest element in the list
  158. */
  159. public int smallest() {
  160. IntList first = new IntList(item(),next());
  161. int min = item();
  162. while(first!=null){
  163. if (min > first.item()){
  164. min = first.item();
  165. }
  166. first = first.next;
  167. }
  168. return min;
  169. }
  170.  
  171. /**
  172. * Returns the sum of squares of all elements in the list.
  173. *
  174. * @return The sum of squares of all elements.
  175. */
  176. public int squaredSum() {
  177. IntList first = new IntList(item(),next());
  178. int sum = 0;
  179. while(first!=null){
  180. sum = sum+(int)Math.pow(first.item(),2);
  181. first = first.next();
  182. }
  183. return sum;
  184. }
  185.  
  186. /**
  187. * Returns a new IntList consisting of L1 followed by L2,
  188. * non-destructively.
  189. *
  190. * @param l1 list to be on the front of the new list.
  191. * @param l2 list to be on the back of the new list.
  192. * @return new list with L1 followed by L2.
  193. */
  194. public static IntList append(IntList l1, IntList l2) {
  195. IntList combinedList = null;
  196. if (l1!=null){
  197. combinedList = new IntList(l1.item());
  198. IntList last = combinedList;
  199. for(int i=1; i<l1.size();i++){
  200. last.next = new IntList(l1.get(i));
  201. last = last.next();
  202. }
  203. if (l2!=null){
  204. last.next = new IntList(l2.item());
  205. last=last.next();
  206. for(int j=1; j<l2.size();j++){
  207. last.next = new IntList(l2.get(j));
  208. last = last.next();
  209. }
  210. }
  211. } else if (l2!=null){
  212. combinedList = new IntList(l2.item());
  213. IntList last = combinedList;
  214. for(int j=1; j<l2.size();j++){
  215. last.next = new IntList(l2.get(j));
  216. last = last.next();
  217. }
  218. }
  219. return combinedList;
  220. }
  221. public static void main(String[] args){
  222. IntList test7 = IntList.list(1, 2, 3, 4, 5);
  223. IntList test8 = null;
  224. IntList test9 = IntList.append(test7, test8);
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement