Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** A data structure to represent a Linked List of Integers.
- * Each IntList represents one node in the overall Linked List.
- *
- * @author James ZHang and John Lin
- */
- public class IntList {
- /** The integer stored by this node. */
- private int item;
- /** The next node in this IntList. */
- private IntList next;
- /** Constructs an IntList storing ITEM and next node NEXT. */
- public IntList(int item, IntList next) {
- this.item = item;
- this.next = next;
- }
- /** Constructs an IntList storing ITEM and no next node. */
- public IntList(int item) {
- this(item, null);
- }
- /** Returns an IntList consisting of the elements in ITEMS.
- * IntList L = IntList.list(1, 2, 3);
- * System.out.println(L.toString()) // Prints (1 2 3) */
- public static IntList list(int... items) {
- /** Check for cases when we have no element given. */
- if (items.length == 0) {
- return null;
- }
- /** Create the first element. */
- IntList head = new IntList(items[0]);
- IntList last = head;
- /** Create rest of the list. */
- for (int i = 1; i < items.length; i++) {
- last.next = new IntList(items[i]);
- last = last.next;
- }
- return head;
- }
- /** Returns the integer stored by this IntList. */
- public int item() {
- return item;
- }
- /** Returns the next node stored by this IntList. */
- public IntList next() {
- return next;
- }
- /**
- * Returns [position]th item in this list. Throws IllegalArgumentException
- * if index out of bounds.
- *
- * @param position, the position of element.
- * @return The element at [position]
- */
- public int get(int position) {
- IntList first = new IntList(item(),next());
- if (position<0){
- //If position < 0, invalid.
- throw new IllegalArgumentException("Sorry, the position is out of range!");
- }
- for (int i=0; i<position;i++){
- if(first.next()==null && i<position){
- //If we've reached the end and we still haven't reached position, invalid.
- throw new IllegalArgumentException("Sorry, the position is out of range!");
- }
- first=first.next();
- }
- return first.item();
- }
- /**
- * Returns the size of the list.
- *
- * @return The size of the list.
- */
- public int size() {
- IntList first = new IntList(item(),next());
- int counter = 1;
- while(first.next()!=null){
- first=first.next();
- counter ++;
- }
- return counter;
- }
- /**
- * Returns the string representation of the list. For the list (1, 2, 3),
- * returns "( 1 2 3 )".
- *
- * @return The String representation of the list.
- */
- public String toString() { //Non-Recursive, Iterative Method
- String stringList = "( ";
- IntList first = new IntList(item(),next());
- while(first!=null){
- stringList = stringList + Integer.toString(first.item()) + " ";
- first=first.next();
- }
- stringList = stringList +")";
- return stringList;
- }
- /**
- * Returns whether this and the given list or object are equal.
- *
- * @param obj, another list (object)
- * @return Whether the two lists are equal.
- */
- public boolean equals(Object obj) {
- if (obj instanceof IntList){ // Is it an IntList? If not, then they don't equal.
- if (((IntList) obj).size()==size()) { //If so, are they the same size?
- IntList firstList = new IntList(item(),next());
- IntList secondList = new IntList(((IntList) obj).item(), ((IntList) obj).next());
- while (firstList!=null){
- if (firstList.item()!=secondList.item()){
- return false;
- }
- firstList = firstList.next();
- secondList = secondList.next();
- }
- } else{
- return false;
- }
- } else{
- return false;
- } //If all conditions are reached, then woohoo! It's true!
- return true;
- }
- /**
- * Adds the given item at the end of the list.
- *
- * @param item, the int to be added.
- */
- public void add(int item) {
- IntList first = new IntList(item(),next());
- IntList toAdd = new IntList(item);
- if (first.next==null){
- this.next=toAdd;
- return;
- }
- while (first.next()!=null){//Let's traverse to the end of the list!
- first=first.next();
- }
- first.next = toAdd;
- }
- /**
- * Returns the smallest element in the list.
- *
- * @return smallest element in the list
- */
- public int smallest() {
- IntList first = new IntList(item(),next());
- int min = item();
- while(first!=null){
- if (min > first.item()){
- min = first.item();
- }
- first = first.next;
- }
- return min;
- }
- /**
- * Returns the sum of squares of all elements in the list.
- *
- * @return The sum of squares of all elements.
- */
- public int squaredSum() {
- IntList first = new IntList(item(),next());
- int sum = 0;
- while(first!=null){
- sum = sum+(int)Math.pow(first.item(),2);
- first = first.next();
- }
- return sum;
- }
- /**
- * Returns a new IntList consisting of L1 followed by L2,
- * non-destructively.
- *
- * @param l1 list to be on the front of the new list.
- * @param l2 list to be on the back of the new list.
- * @return new list with L1 followed by L2.
- */
- public static IntList append(IntList l1, IntList l2) {
- IntList combinedList = null;
- if (l1!=null){
- combinedList = new IntList(l1.item());
- IntList last = combinedList;
- for(int i=1; i<l1.size();i++){
- last.next = new IntList(l1.get(i));
- last = last.next();
- }
- if (l2!=null){
- last.next = new IntList(l2.item());
- last=last.next();
- for(int j=1; j<l2.size();j++){
- last.next = new IntList(l2.get(j));
- last = last.next();
- }
- }
- } else if (l2!=null){
- combinedList = new IntList(l2.item());
- IntList last = combinedList;
- for(int j=1; j<l2.size();j++){
- last.next = new IntList(l2.get(j));
- last = last.next();
- }
- }
- return combinedList;
- }
- public static void main(String[] args){
- IntList test7 = IntList.list(1, 2, 3, 4, 5);
- IntList test8 = null;
- IntList test9 = IntList.append(test7, test8);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement