Skip to content
Browse files

clean

  • Loading branch information...
1 parent ddc53b4 commit 221f36655a709f5da20af13538ac875fea990503 @willawill committed Jan 4, 2012
Showing with 478 additions and 0 deletions.
  1. +28 −0 DeleteMidEle.java
  2. +11 −0 EnumType.java
  3. +223 −0 LinkedList/LinkedList.java
  4. +23 −0 LinkedList/LinkedListNode.java
  5. +63 −0 LinkedList/LinkedStack.java
  6. +4 −0 LinkedList/reverseAdd.java
  7. +15 −0 XORswap.py
  8. +100 −0 quickSelection.java
  9. +11 −0 reverseString.py
View
28 DeleteMidEle.java
@@ -0,0 +1,28 @@
+
+
+public class DeleteMidEle{
+ private LinkedList l;
+
+ public DeleteMidEle(LinkedList l){
+ this.l = l;
+ }
+ public void deleteMidEle(Object s){
+ LinkedListNode newS = new LinkedListNode(s);
+ LinkedListNode temp = newS.getNext();
+ newS.setValue(temp.getValue());
+ newS.setNext(temp.getNext());
+
+ }
+ public static void main(String[] args){
+ LinkedList l = new LinkedList();
+ l.add(5);
+ l.add(6);
+ l.add(7);
+ l.add(11);
+ DeleteMidEle dme = new DeleteMidEle(l);
+ dme.deleteMidEle(7);
+ System.out.println(l.toString());
+
+ }
+
+}
View
11 EnumType.java
@@ -0,0 +1,11 @@
+public class Type{
+ enum type{
+ KING, QUEENS;
+}
+
+public static void main(String[] args){
+ for(type t: type.values()){
+ System.out.println(t);
+ }
+}
+}
View
223 LinkedList/LinkedList.java
@@ -0,0 +1,223 @@
+import java.util.*;
+
+public class LinkedList{
+ private LinkedListNode head;
+ private int size;
+
+ public LinkedList(){
+ this.head = null;
+ this.size = 0;
+
+}
+ public void add(Object s){
+ LinkedListNode temp = new LinkedListNode(s);
+ if (this.size == 0){
+ head = temp;
+ this.size++;
+ }
+ else{
+ LinkedListNode current = head;
+ while (current.getNext() != null){
+ current = current.getNext();
+ }
+ current.setNext(temp);
+ this.size++;
+ }
+}
+ public void insert(Object s, int index){
+ LinkedListNode temp = new LinkedListNode(s);
+
+ //Invalid index.It also can be treated as a head.
+ if (index < 0 ){
+ temp.setNext(head);
+ head = temp;
+ this.size ++;
+ }
+ //Any index exceeds the size will be appended to the tail.
+ else if (index > size){
+ this.add(s);
+ }
+ else{
+ LinkedListNode current = head;
+ LinkedListNode previous = null;
+ for( int i = 0; i < index - 1 && current.getNext() != null ; i++){
+ current = current.getNext();
+
+ }
+ previous = current;
+ current = current.getNext();
+ previous.setNext(temp);
+ temp.setNext(current);
+ this.size++;
+ }
+}
+ public Object get(int index){
+ if (index < 0 || index > this.size){
+ return null;
+}
+ LinkedListNode current = head;
+ for (int i = 1 ; i <= index ; i++){
+ current = current.getNext();
+
+ }
+ return current.getValue();
+}
+ public boolean remove(Object s){
+ LinkedListNode current = head;
+ LinkedListNode previous = null;
+ while(current != null){
+ if (current.getValue().equals(s)){
+ previous.setNext(current.getNext());
+ this.size --;
+ return true;
+ }
+ previous = current;
+ current = current.getNext();
+ }
+ return false;
+}
+ public void removeDuplicates(){
+ LinkedListNode current, previous;
+ current = head;
+ previous = null;
+ HashSet table = new HashSet();
+ while (current != null){
+ if (table.contains(current.getValue())) {
+ previous.setNext(current.getNext());
+ }
+ else{
+ previous = current;
+ table.add(current.getValue());
+ }
+ current = current.getNext();
+
+ }
+}
+
+ public String toString(){
+ LinkedListNode current = this.head;
+ String output ="The elements are ";
+ while (current != null){
+ output += " " + current.getValue();
+ current = current.getNext();
+ }
+ return output;
+
+ }
+
+ public int getSize(){
+ return this.size;
+}
+ public LinkedListNode getHead(){
+ return this.head;
+ }
+
+ public static void main(String[] args){
+ LinkedList l = new LinkedList();
+ l.add(1);
+ l.add(2);
+ l.add(3);
+ l.add(4);
+ l.add(5);
+ l.insert(6,2);
+ System.out.println(l.remove(3));
+
+ //l.removeDuplicates();
+ l.removeDupNoHashset();
+ l.deleteMidEle(4);
+
+ System.out.println(l.returnKth3(3).getValue());
+ System.out.println(l.get(2));
+ System.out.println(l.toString());
+
+}
+public int count = 0;
+ public void removeDupNoHashset(){
+ if (head == null) return;
+ LinkedListNode current, previous;
+ LinkedListNode runner;
+ previous = head;
+ current = previous.getNext();
+
+ while (current != null){
+ runner = head; //Look backward to find duplicates.
+ while ( runner != current){
+ if (runner.getValue() == current.getValue()){
+ LinkedListNode temp = current.getNext();
+ previous.setNext(temp);
+ current = temp;
+ break;
+
+ }
+ count ++;
+ runner = runner.getNext();
+
+ }
+
+ if (runner == current){
+ previous = current;
+ current = current.getNext();
+ count ++;
+
+ }
+
+ }
+ }
+
+public LinkedListNode returnKth(int k){
+ if ((k < 0)||(k > this.size)){
+ return null;
+ }
+ int index = 0;
+ LinkedListNode current = this.head;
+ while (current != null){
+ if (index == (this.size-k)){
+ return current;
+ }
+ index ++;
+ current = current.getNext();
+
+ }
+ return null;
+}
+
+private class IntWrapper{
+ public int value = 0;
+}
+public LinkedListNode returnKth2(LinkedListNode node , int k, IntWrapper i){
+ if ((node == null)|| k < 0) return null;
+ LinkedListNode n = returnKth2(node.getNext(),k, i);
+ i.value = i.value + 1;
+ if ( i.value == k){
+ return node;
+ }
+ return n;
+}
+//Iteratively return the last Kth element.
+public LinkedListNode returnKth3(int k){
+ LinkedListNode p1 = head;
+ LinkedListNode p2 = head;
+ for (int i = 0 ; i < k ; i++){
+ p2 = p2.getNext();
+ }
+ while (p2 != null){
+ p2 = p2.getNext();
+ p1 = p1.getNext();
+ }
+ return p1;
+
+}
+ public void deleteMidEle(Object s){
+ LinkedListNode newS = new LinkedListNode(s);
+ LinkedListNode temp = newS.getNext();
+ try{
+ newS.setValue(temp.getValue());
+ newS.setNext(temp.getNext());
+ this.size --;
+
+ }
+ catch (NullPointerException e){
+ System.out.println("This may be the last element.");
+ }
+}
+}
View
23 LinkedList/LinkedListNode.java
@@ -0,0 +1,23 @@
+public class LinkedListNode{
+ Object value;
+ LinkedListNode next;
+
+
+ public LinkedListNode(Object _o){
+ value = _o;
+ next = null;
+
+}
+ public LinkedListNode getNext(){
+ return next;
+}
+ public void setNext(LinkedListNode node){
+ next = node;
+}
+ public Object getValue(){
+ return value;
+}
+ public void setValue(Object _o){
+ this.value = _o;
+}
+}
View
63 LinkedList/LinkedStack.java
@@ -0,0 +1,63 @@
+public class linkedStack(){
+
+
+private LinkedNode top;
+private int count;
+
+public linkedStack(){
+ count = 0;
+ top = null;
+}
+
+public void push(T element){
+ LinedNode n = new LinkedNode(element);
+ top.setNext(n);
+ top = n;
+ count ++;
+}
+
+public T pop(){
+ if (top == null){
+ throw new EmptyCollectionException(“Stack”);
+
+}
+ T element = top.getElement();
+ top = top.getNext();// top = top.next. Because first points to the next.
+ count --;
+ return element;
+
+}
+
+}
+
+class LinkedNode<T>(){
+
+private LinkedNode<T> next;
+private T element;
+
+public LinkedNode(){
+ next = null;
+ element = null;
+
+}
+
+public LinkedNode(T element){
+ next = null;
+ this.element = element;
+
+}
+public LinkedNode<T> getNext(){
+ return next;
+}
+public void setNext(LinkedNode<T> n){
+ this.next = n;
+}
+
+public T getElement(){
+ return element;
+}
+
+public void setElement(T element){
+ this.element = element;
+}
+}
View
4 LinkedList/reverseAdd.java
@@ -0,0 +1,4 @@
+public class reverseAdd{
+ private
+
+}
View
15 XORswap.py
@@ -0,0 +1,15 @@
+#!/usr/bin/python
+
+a = 6
+b = 7
+a = a+b
+b = a-b
+a = a-b
+print a, b
+
+a^=b
+print a,b
+b^=a
+print a,b
+a^=b
+print a,b
View
100 quickSelection.java
@@ -0,0 +1,100 @@
+public class quick{
+
+/**
+ * Quick selection algorithm.
+ * Places the kth smallest item in a[k-1].
+ * @param a an array of Comparable items.
+ * @param k the desired rank (1 is minimum) in the entire array.
+ */
+ public static void quickSelect( Comparable [ ] a, int k ) {
+ quickSelect( a, 0, a.length - 1, k );
+ }
+
+ /**
+ * Internal selection method that makes recursive calls.
+ * Uses median-of-three partitioning and a cutoff of 10.
+ * Places the kth smallest item in a[k-1].
+ * @param a an array of Comparable items.
+ * @param low the left-most index of the subarray.
+ * @param high the right-most index of the subarray.
+ * @param k the desired rank (1 is minimum) in the entire array.
+ */
+ private static void quickSelect( Comparable [ ] a, int low, int high, int k ) {
+ if( low + CUTOFF > high )
+ insertionSort( a, low, high );
+ else {
+ // Sort low, middle, high
+ int middle = ( low + high ) / 2;
+ if( a[ middle ].compareTo( a[ low ] ) < 0 )
+ swapReferences( a, low, middle );
+ if( a[ high ].compareTo( a[ low ] ) < 0 )
+ swapReferences( a, low, high );
+ if( a[ high ].compareTo( a[ middle ] ) < 0 )
+ swapReferences( a, middle, high );
+
+ // Place pivot at position high - 1
+ swapReferences( a, middle, high - 1 );
+ Comparable pivot = a[ high - 1 ];
+
+ // Begin partitioning
+ int i, j;
+ for( i = low, j = high - 1; ; ) {
+ while( a[ ++i ].compareTo( pivot ) < 0 )
+ ;
+ while( pivot.compareTo( a[ --j ] ) < 0 )
+ ;
+ if( i >= j )
+ break;
+ swapReferences( a, i, j );
+ }
+
+ // Restore pivot
+ swapReferences( a, i, high - 1 );
+
+ // Recurse; only this part changes
+ if( k <= i )
+ quickSelect( a, low, i - 1, k );
+ else if( k > i + 1 )
+ quickSelect( a, i + 1, high, k );
+ }
+ }
+
+
+ /**
+ * Internal insertion sort routine for subarrays
+ * that is used by quicksort.
+ * @param a an array of Comparable items.
+ * @param low the left-most index of the subarray.
+ * @param n the number of items to sort.
+ */
+ private static void insertionSort( Comparable [ ] a, int low, int high ) {
+ for( int p = low + 1; p <= high; p++ ) {
+ Comparable tmp = a[ p ];
+ int j;
+
+ for( j = p; j > low && tmp.compareTo( a[ j - 1 ] ) < 0; j-- )
+ a[ j ] = a[ j - 1 ];
+ a[ j ] = tmp;
+ }
+ }
+
+
+ private static final int CUTOFF = 10;
+
+ /**
+ * Method to swap to elements in an array.
+ * @param a an array of objects.
+ * @param index1 the index of the first object.
+ * @param index2 the index of the second object.
+ */
+ public static final void swapReferences( Object [ ] a, int index1, int index2 ) {
+ Object tmp = a[ index1 ];
+ a[ index1 ] = a[ index2 ];
+ a[ index2 ] = tmp;
+ }
+public static void main(){
+ int[] a = {5,6,2,4,8,12,9,-5};
+ quick.quickSelect(a, 2);
+
+}
+}
View
11 reverseString.py
@@ -0,0 +1,11 @@
+def reverseString(s):
+ if (s == ""):
+ return s
+ else:
+ return reverseString(s[1:])+s[0]
+
+
+print reverseString("abcd")
+t = "abcd"
+backward = lambda t: t[-1] + backward(t[:-1]) if t else t
+print backward(t)

0 comments on commit 221f366

Please sign in to comment.
Something went wrong with that request. Please try again.