CSC300: XFixedCapacityStackOfStrings [9/15] Previous pageContentsNext page

file:XFixedCapacityStackOfStrings.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package algs13;

import stdlib.*;

public class XFixedCapacityStackOfStrings {
  private final String[] a; // holds the items
  private int N;            // number of items in stack
    // a[0]..a[N-1] are non null
  // a[N]..a[a.length-1] are null
  public XFixedCapacityStackOfStrings (int capacity) {
    this.a = new String[capacity];
    this.N = 0;
  }
  public int size ()        { return N; }
  public boolean isEmpty () { return (N == 0); }
  public void push (String item) {
    if (item == null) throw new IllegalArgumentException ();
    a[N] = item;
    N++;
  }
  public String pop () {
    N--;
    String result = a[N];
    a[N] = null;
    return result;
  }

  public static void main(String[] args) {
    Trace.showObjectIdsRedundantly (true);
    //Trace.showBuiltInObjects (true);
    Trace.drawStepsOfMethod ("main");
    Trace.run ();
    XFixedCapacityStackOfStrings stack = new XFixedCapacityStackOfStrings (7);
    stack.push ("a");
    stack.push ("b");
    stack.push ("c");
    stack.push ("d");
    while (!stack.isEmpty()) {
      StdOut.println (stack.pop ());
    }
  }
}

Previous pageContentsNext page