|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Stack.java | 66.7% | 86.7% | 100% | 85.7% |
|
||||||||||||||
| 1 |
package baseCode.dataStructure;
|
|
| 2 |
|
|
| 3 |
/**
|
|
| 4 |
* <p>
|
|
| 5 |
* Simple Stack implementation
|
|
| 6 |
* </p>
|
|
| 7 |
* <p>
|
|
| 8 |
* Copyright (c) 2004
|
|
| 9 |
* </p>
|
|
| 10 |
* <p>
|
|
| 11 |
* Institution: Columbia University
|
|
| 12 |
* </p>
|
|
| 13 |
*
|
|
| 14 |
* @author Paul Pavlidis
|
|
| 15 |
* @version $Id: Stack.java,v 1.5 2004/07/29 08:38:49 pavlidis Exp $
|
|
| 16 |
* @deprecated -- use java.util.List instead.
|
|
| 17 |
*/
|
|
| 18 |
public class Stack { |
|
| 19 |
|
|
| 20 |
private Object[] stack;
|
|
| 21 |
private int top; |
|
| 22 |
private final static int DEFAULTCAPACITY = 10000; |
|
| 23 |
|
|
| 24 |
/**
|
|
| 25 |
* Build a stack with the default capacity.
|
|
| 26 |
*/
|
|
| 27 | 5 |
public Stack() {
|
| 28 | 5 |
this( DEFAULTCAPACITY );
|
| 29 |
} |
|
| 30 |
|
|
| 31 |
/**
|
|
| 32 |
* Build a stack with a given capacity.
|
|
| 33 |
*
|
|
| 34 |
* @param capacity int
|
|
| 35 |
*/
|
|
| 36 | 10 |
public Stack( int capacity ) { |
| 37 | 10 |
stack = new Object[capacity];
|
| 38 |
} |
|
| 39 |
|
|
| 40 |
/**
|
|
| 41 |
* Remove the most recently added item.
|
|
| 42 |
*
|
|
| 43 |
* @return Object
|
|
| 44 |
*/
|
|
| 45 | 6 |
public Object pop() {
|
| 46 | 6 |
if ( isEmpty() ) {
|
| 47 | 0 |
return null; |
| 48 |
} |
|
| 49 | 6 |
Object topObj = top(); |
| 50 | 6 |
stack[top--] = null;
|
| 51 | 6 |
return topObj;
|
| 52 |
|
|
| 53 |
} |
|
| 54 |
|
|
| 55 |
/**
|
|
| 56 |
* Add an item to the stack.
|
|
| 57 |
*
|
|
| 58 |
* @param obj Object
|
|
| 59 |
*/
|
|
| 60 | 36 |
public void push( Object obj ) { |
| 61 | 36 |
if ( isFull() ) {
|
| 62 | 1 |
throw new IndexOutOfBoundsException( "Stack overflow" ); |
| 63 |
} |
|
| 64 | 35 |
stack[++top] = obj; |
| 65 |
} |
|
| 66 |
|
|
| 67 |
/**
|
|
| 68 |
* Get the most recently added item, without removing it.
|
|
| 69 |
*
|
|
| 70 |
* @return Object
|
|
| 71 |
*/
|
|
| 72 | 7 |
public Object top() {
|
| 73 | 7 |
if ( isEmpty() ) {
|
| 74 | 0 |
return null; |
| 75 |
} |
|
| 76 | 7 |
return stack[top];
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
/**
|
|
| 80 |
* @return boolean
|
|
| 81 |
*/
|
|
| 82 | 13 |
public boolean isEmpty() { |
| 83 | 13 |
return top == -1;
|
| 84 |
} |
|
| 85 |
|
|
| 86 |
/**
|
|
| 87 |
* @return boolean
|
|
| 88 |
*/
|
|
| 89 | 36 |
public boolean isFull() { |
| 90 | 36 |
return top == stack.length - 1;
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
} |
|
||||||||||