Class Name: 
kiasan.examples.stack.StackLi

Report Rendered: Mon May 04 12:43:42 CDT 2009, by Sireum/Kiasan for Java v0.1.20090504

Methods Covered:
PercentRatio
Class / Method T E Instruction Coverage Branch Coverage Time
2 1
  35/35
  100%
  4/4
  100%
0.201s
3 0
  18/18
  100%
  0/0
  100%
0.215s
Total
5 1
  53/53
  100%
  4/4
  100%
0.416s
Source Code:
1   package kiasan.examples.stack;
2   
3   import kiasan.examples.common.Underflow;
4   
5   // StackLi class
6   //
7   // CONSTRUCTION: with no initializer
8   //
9   // ******************PUBLIC OPERATIONS*********************
10  // void push( x )         --> Insert x
11  // void pop( )            --> Remove most recently inserted item
12  // Object top( )          --> Return most recently inserted item
13  // Object topAndPop( )    --> Return and remove most recent item
14  // boolean isEmpty( )     --> Return true if empty; else false
15  // boolean isFull( )      --> Always returns false
16  // void makeEmpty( )      --> Remove all items
17  // ******************ERRORS********************************
18  // pop on empty stack
19  
20  /**
21   * List-based implementation of the stack.
22   * 
23   * @author Mark Allen Weiss
24   */
25  public class StackLi {
26    public static void main(final String[] args) {
27      final StackLi s = new StackLi();
28  
29      for (int i = 0; i < 10; i++) {
30        s.push(new Integer(i));
31      }
32  
33      while (!s.isEmpty()) {
34        System.out.println(s.topAndPop());
35      }
36    }
37  
38    private ListNode topOfStack;
39  
40    /**
41     * Construct the stack.
42     */
43    public StackLi() {
44      this.topOfStack = null;
45    }
46  
47    private boolean contains(final Object e) {
48      ListNode temp = this.topOfStack;
49      while (temp != null) {
50        if (== temp.element) {
51          return true;
52        }
53        temp = temp.next;
54      }
55      return false;
56    }
57  
58    boolean isAcyclic() {
59      final StackLi ll = new StackLi();
60      ListNode temp = this.topOfStack;
61      while (temp != null) {
62        if (ll.contains(temp)) {
63          return false;
64        }
65        ll.topOfStack = new ListNode(temp, ll.topOfStack);
66        temp = temp.next;
67      }
68      return true;
69    }
70  
71    /**
72     * Test if the stack is logically empty.
73     * 
74     * @return true if empty, false otherwise.
75     */
76    public boolean isEmpty() {
77      return this.topOfStack == null;
78    }
79  
80    /**
81     * Test if the stack is logically full.
82     * 
83     * @return false always, in this implementation.
84     */
85    public boolean isFull() {
86      return false;
87    }
88  
89    /**
90     * Make the stack logically empty.
91     */
92    public void makeEmpty() {
93      this.topOfStack = null;
94    }
95  
96    /**
97     * Remove the most recently inserted item from the stack.
98     * 
99     * @exception Underflow
100    *              if the stack is empty.
101    */
102   //@ requires this.isAcyclic();
103   //@ ensures this.isAcyclic();
104   public void pop() throws Underflow {
105     if (isEmpty()) {
106       throw new Underflow();
107     }
108     this.topOfStack = this.topOfStack.next;
109   }
110 
111   /**
112    * Insert a new item into the stack.
113    * 
114    * @param x
115    *          the item to insert.
116    */
117   //@ requires this.isAcyclic();
118   //@ ensures this.isAcyclic() && this.topOfStack.element == x;
119   public void push(final Object x) {
120     this.topOfStack = new ListNode(x, this.topOfStack);
121   }
122 
123   /**
124    * Get the most recently inserted item in the stack. Does not alter the stack.
125    * 
126    * @return the most recently inserted item in the stack, or null, if empty.
127    */
128   public Object top() {
129     if (isEmpty()) {
130       return null;
131     }
132     return this.topOfStack.element;
133   }
134 
135   /**
136    * Return and remove the most recently inserted item from the stack.
137    * 
138    * @return the most recently inserted item in the stack, or null, if empty.
139    */
140   public Object topAndPop() {
141     if (isEmpty()) {
142       return null;
143     }
144 
145     final Object topItem = this.topOfStack.element;
146     this.topOfStack = this.topOfStack.next;
147     return topItem;
148   }
149 }