This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reference : http://hawstein.com/posts/3.4.html | |
import java.util.Stack; | |
public class Hanoi { | |
private Stack<Operation> stack; | |
private class Operation { | |
private int start; | |
private int end; | |
private String src; | |
private String bridge; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Stack; | |
import java.util.ArrayList; | |
public class SetOfStacks<T> { | |
private ArrayList<myStack> stacks; | |
private int threshold; | |
private int currIdx; | |
private class myStack { | |
Stack<T> stack; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; //LinkedList in Java is double-linked list | |
import java.util.ListIterator; | |
import java.util.Stack; | |
public class solution { | |
public static<T> boolean isPalindrome(LinkedList<T> list) { | |
if (list == null) throw new IllegalArgumentException(); | |
Stack<T> reverse = new Stack<T>(); | |
int N = 0; | |
for (T element : list) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
public class partition { | |
public static<T extends Comparable<? super T>> LinkedList<T> parti(LinkedList<T> list, T pivot) { | |
LinkedList<T> lessThan = new LinkedList<T>(); | |
LinkedList<T> equal = new LinkedList<T>(); | |
LinkedList<T> largerThan = new LinkedList<T>(); | |
int cmp; | |
for (T element : list) { | |
cmp = element.compareTo(pivot); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Stack; | |
import java.util.Random; | |
public class minStack<T extends Comparable<? super T>> { | |
Stack<T> data; | |
Stack<T> min; | |
T minimum; | |
int N; | |
public minStack() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* http://algs4.cs.princeton.edu/13stacks/ | |
//* [statck1 element, statck2 element, statck3 element, statck1 element,statck2 element,...,statck3 element] | |
//* double the array size when one stack is full | |
//* shrink the array size to half when all three stacks are one-quarter size full | |
//* time complexity: average O(1) for push() and pop() | |
import java.lang.reflect.Array; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class TripleStack<Item> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.HashSet; | |
public class CircularList<Item> { | |
private Node first; | |
private Node last; | |
private class Node { | |
private Item item; | |
private Node next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
import java.util.ListIterator; | |
import java.util.Random; | |
public class add { | |
public static LinkedList<Integer> addList(LinkedList<Integer> a, | |
LinkedList<Integer> b) { | |
LinkedList<Integer> result = new LinkedList<Integer>(); | |
ListIterator<Integer> iterA = a.listIterator(); | |
ListIterator<Integer> iterB = b.listIterator(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LinkedList<Item> { | |
private Node<Item> first; | |
private Node<Item> last; | |
private int N; | |
public LinkedList() { | |
this.N = 0; | |
} | |
public void add(Node<Item> node) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
public class nthToLast { | |
public static<Item> Item find(LinkedList<Item> list, int n) { | |
int N = list.size(); | |
if ( n < 1 || n > N) return null; | |
int idx = N - n; | |
return list.get(idx); | |
} | |