// Allison Obourn // CSE 143 - Lecture 8 // A LinkedIntList stores an ordered list of integers. // Final review: rearrange public class LinkedIntList implements IntList { private ListNode front; // Initializes a new empty list public LinkedIntList() { front = null; } // Build a new list with nodes of values n to 0. // Pre: n >= 0, throws an IllegalArgumentException otherwise public LinkedIntList(int count) { if (count < 0) { throw new IllegalArgumentException(); } for(int i = 0; i <= count; i++) { front = new ListNode(i, front); } } // rearranges the order of a list of integers so that all of the values in // even-numbered positions appear in reverse order followed by all of the // values in odd-numbered positions in forward order. // If the list has fewer than two elements, it should be unchanged. // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -> [8, 6, 4, 2, 0, 1, 3, 5, 7, 9] // 1) are there any easy special cases? Deal with fewer than 2 elements. // 2) draw lots of pictures // here, we saw that dealing with odd indexes was easy // even indexes seemed harder because of the reversing // 3) write any easy code, leaving lots of space // write repeating code "inside out" -- figure out what one iteration is // then add a loop. Use the statements in the loop to fix bounds // here, we access current.next.next in the loop so current.next can't be // null // 4) finish any tricky code left aside earlier // we went back to playing around with pictures and found we could // bring elements to the front to reverse public void rearrange() { if(front != null && front.next != null) { ListNode current = front.next; while(current != null && current.next != null) { ListNode temp = current.next; current.next = current.next.next; temp.next = front; front = temp; current = current.next; } } } // Adds a value at a given index. // Pre: 0 <= index <= size, throws a NullPointerException if index > size. public void add(int index, int value) { if (index == 0) { front = new ListNode(value, front); } else { ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } ListNode temp = new ListNode(value, current.next); current.next = temp; // also ok: current.next = new ListNode(value, current.next); } } // Adds the value in sorted (non-decreasing) order. // Pre: list is sorted // Post: list is sorted public void addSorted(int value) { if(front == null || value < front.data) { front = new ListNode(value, front); } else { ListNode current = front; while(current.next != null && current.next.data < value) { current = current.next; } current.next = new ListNode(value, current.next); } } // Returns the value at the given index. // Pre: 0 <= index < size public int get(int index) { ListNode current = front; for(int i = 0; i < index; i++) { current = current.next; } return current.data; } // Returns a comma-separated String representation of this list. public String toString() { if (front == null) { return "[]"; } else { String result = "[" + front.data; ListNode current = front.next; while (current != null) { result += ", " + current.data; current = current.next; } result += "]"; return result; } } // Returns the size of this list (inefficient -- could use a size field). public int size() { ListNode current = front; int size = 0; while(current != null) { size++; current = current.next; } return size; } // True if the list is empty, false otherwise public boolean isEmpty() { return front == null; } // Removes the value at the given index. // Pre: 0 <= index < size public void remove(int index) { if (index == 0) { front = front.next; } else { ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; } } private class ListNode { public int data; public ListNode next; // Creates a terminal ListNode containing 0 as integer data. public ListNode() { this(0, null); } // Creates a terminal ListNode with the specified integer data. public ListNode(int data) { this(data, null); } // Creates a ListNode with the specified integer data and next node. public ListNode(int data, ListNode next) { this.data = data; this.next = next; } } }