Skip to content

Instantly share code, notes, and snippets.

@ZacBlanco
Created October 3, 2017 20:29
Show Gist options
  • Save ZacBlanco/bdca65147a67d0cfaa3bae2b35872c26 to your computer and use it in GitHub Desktop.
Save ZacBlanco/bdca65147a67d0cfaa3bae2b35872c26 to your computer and use it in GitHub Desktop.
CS112 Study Group 10/3/17
package io.blanco;
import java.util.NoSuchElementException;
public class List<T extends Comparable<T>> {
private Node<T> head;
private int size;
public int size() {
return this.size;
}
public Node<T> head() {
return this.head;
}
public void add(T item) {
Node<T> it = new Node<T>(item, null);
if (head == null) {
head = it;
this.size++;
return;
}
Node<T> tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
tmp.next = it;
this.size++;
}
//Return new list with all entries in first that are not in second
public List<T> difference(List<T> other) {
Node<T> l1 = this.head;
Node<T> l2 = other.head;
List<T> newList = new List<T>();
while(l1 != null && l2 != null) {
if (l1.data.compareTo(l2.data) < 0) {
newList.add(l1.data);
l1 = l1.next;
} else if (l1.data.compareTo(l2.data) > 0) {
l2 = l2.next;
} else {
l2 = l2.next;
l1 = l1.next;
}
}
while (l1 != null) {
newList.add(l1.data);
l1 = l1.next;
}
return newList;
}
public T get(int index) throws NoSuchElementException {
if (index < 0 || index >= this.size || head == null) {
throw new NoSuchElementException("List is size " + this.size);
}
int count = 1;
Node<T> tmp = head;
while(tmp != null) {
if (count == index) {
break;
}
count++;
tmp = tmp.next;
}
return tmp.data;
}
public String toString() {
if (head != null) {
return this.head.toString();
} else {
return "Empty List";
}
}
}
package io.blanco;
public class Node<T> {
T data;
Node<T> next;
public Node(T data, Node<T> next) {
this.data = data;
this.next = next;
}
public String toString() {
String nxt = "/";
if (this.next != null){
nxt = this.next.toString();
}
return this.data.toString() + " -> " + nxt;
}
}
package io.blanco;
import java.util.NoSuchElementException;
public class Tester {
public static void main(String[] args) {
demo1();
// demo2();
}
public static void demo1() {
List<Integer> l = new List<Integer>();
try{
l.get(-1);
} catch(NoSuchElementException e) {}
try {
l.get(10);
} catch(NoSuchElementException e) {}
l.add(2);
l.add(1);
l.add(4);
System.out.println(l.toString());
System.out.println(l.get(2));
l.add(16);
System.out.println(l.toString());
System.out.println(l.get(1));
l.add(32);
System.out.println(l.toString());
}
public static void demo2() {
List<Integer> l1 = new List<>();
List<Integer> l2 = new List<>();
l1.add(2);
l1.add(4);
l2.add(2);
l2.add(4);
System.out.println(l1);
System.out.println(l2);
System.out.println(l1.difference(l1));
l2.add(8);
l2.add(16);
System.out.println(l1.difference(l2));
System.out.println(l2.difference(l1));
}
// Decimal to Binary
public static String D2B(int x) {
return x > 0 ? D2B(x/2) + (x % 2) : "";
}
// Binary to Decimal
public static int B2D(String b) {
return b.length() > 0 ? (int) (Integer.parseInt(b.substring(0, 1))*Math.pow(2, b.length()-1) + B2D(b.substring(1))) : 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment