mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
@ -8,32 +8,31 @@ package DataStructures.Stacks;
|
|||||||
* of an array implementation of a Stack. So an element can only be added/removed
|
* of an array implementation of a Stack. So an element can only be added/removed
|
||||||
* from the end of the array. In theory stack have no fixed size, but with an
|
* from the end of the array. In theory stack have no fixed size, but with an
|
||||||
* array implementation it does.
|
* array implementation it does.
|
||||||
*
|
|
||||||
* @author Unknown
|
|
||||||
*/
|
*/
|
||||||
public class StackArray {
|
public class StackArray {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main method
|
* Driver Code
|
||||||
*
|
|
||||||
* @param args Command line arguments
|
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Declare a stack of maximum size 4
|
// Declare a stack of maximum size 4
|
||||||
StackArray myStackArray = new StackArray(4);
|
StackArray myStackArray = new StackArray(4);
|
||||||
|
|
||||||
|
assert myStackArray.isEmpty();
|
||||||
|
assert !myStackArray.isFull();
|
||||||
|
|
||||||
// Populate the stack
|
// Populate the stack
|
||||||
myStackArray.push(5);
|
myStackArray.push(5);
|
||||||
myStackArray.push(8);
|
myStackArray.push(8);
|
||||||
myStackArray.push(2);
|
myStackArray.push(2);
|
||||||
myStackArray.push(9);
|
myStackArray.push(9);
|
||||||
|
|
||||||
System.out.println("*********************Stack Array Implementation*********************");
|
assert !myStackArray.isEmpty();
|
||||||
System.out.println(myStackArray.isEmpty()); // will print false
|
assert myStackArray.isFull();
|
||||||
System.out.println(myStackArray.isFull()); // will print true
|
assert myStackArray.peek() == 9;
|
||||||
System.out.println(myStackArray.peek()); // will print 9
|
assert myStackArray.pop() == 9;
|
||||||
System.out.println(myStackArray.pop()); // will print 9
|
assert myStackArray.peek() == 2;
|
||||||
System.out.println(myStackArray.peek()); // will print 2
|
assert myStackArray.size() == 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,7 +61,7 @@ public class StackArray {
|
|||||||
public StackArray() {
|
public StackArray() {
|
||||||
this(DEFAULT_CAPACITY);
|
this(DEFAULT_CAPACITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
@ -162,4 +161,13 @@ public class StackArray {
|
|||||||
public void makeEmpty() { // Doesn't delete elements in the array but if you call
|
public void makeEmpty() { // Doesn't delete elements in the array but if you call
|
||||||
top = -1; // push method after calling makeEmpty it will overwrite previous values
|
top = -1; // push method after calling makeEmpty it will overwrite previous values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return size of stack
|
||||||
|
*
|
||||||
|
* @return size of stack
|
||||||
|
*/
|
||||||
|
public int size() {
|
||||||
|
return top + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user