Skip to content

Instantly share code, notes, and snippets.

@chouclee
Created June 20, 2014 04:32
Show Gist options
  • Save chouclee/184a1b3674cd88df29f5 to your computer and use it in GitHub Desktop.
Save chouclee/184a1b3674cd88df29f5 to your computer and use it in GitHub Desktop.
[CC150][3.1] Describe how you could use a single array to implement three stacks
//* 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> {
public stack stack1;
public stack stack2;
public stack stack3;
private stack[] stacks;
private Item[] data;
private boolean[] oneQuarterFull;
@SuppressWarnings("unchecked")
public TripleStack() {
data = (Item[]) new Object[6];
//stacks = (stack[]) new Object[3]; // doesn't work!!
stacks = (stack[]) Array.newInstance(stack.class, 3); // works
stack1 = new stack(0);
stack2 = new stack(1);
stack3 = new stack(2);
stacks[0] = stack1;
stacks[1] = stack2;
stacks[2] = stack3;
oneQuarterFull = new boolean[3];
}
private class stack implements Iterable<Item> {
private int N; //number of elements in stack1
private int offset;
public stack(int os) {
this.offset = os;
this.N = 0;
}
public void push(Item item) {
data[this.N*3 + this.offset] = item;
this.N++;
if (this.N == data.length/3)
resize(6*data.length);
}
public Item pop() {
Item popOut = data[(this.N - 1)*3 + this.offset];
this.N--;
if (this.N == data.length/12) {
oneQuarterFull[this.offset] = true;
if (oneQuarterFull[0] && oneQuarterFull[1] && oneQuarterFull[2]) {
resize(data.length/3);
oneQuarterFull[0] = false;
oneQuarterFull[1] = false;
oneQuarterFull[2] = false;
}
}
return popOut;
}
public Iterator<Item> iterator() {
return new ArrayIterator();
}
private class ArrayIterator implements Iterator<Item> {
private int i = 0;
public boolean hasNext() { return i < N; }
public void reomve() { throw new UnsupportedOperationException();}
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
return data[3*i++ + offset];
}
}
}
@SuppressWarnings("unchecked")
private void resize(int max) {
Item[] temp = (Item[]) new Object[max];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < stacks[i].N; j++)
temp[j*3 + stacks[i].offset] = data[j*3 + stacks[i].offset];
}
data = temp;
}
public String toString() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < 3; i++) {
s.append("Stack " + (i+1) + ": ");
for (Item it : stacks[i])
s.append(it.toString() + " ");
s.append("\n");
}
return s.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TripleStack<Integer> test = new TripleStack<Integer>();
for (int i = 0; i < 5; i++) test.stack1.push(i);
for (int i = 0; i < 5; i++) test.stack2.push(i);
for (int i = 0; i < 5; i++) test.stack3.push(i);
System.out.println(test.toString());
System.out.println("************POP Test**************");
System.out.print("Stack 1 - pop :");
for (int i = 0; i < 5; i++)
System.out.print(test.stack1.pop() + " ");
System.out.print("\n");
System.out.print("Stack 2 - pop :");
for (int i = 0; i < 5; i++)
System.out.print(test.stack2.pop() + " ");
System.out.print("\n");
System.out.print("Stack 3 - pop :");
for (int i = 0; i < 5; i++)
System.out.print(test.stack3.pop() + " ");
System.out.print("\n");
System.out.println("\n"+test.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment