commit
3fb71bc89b
@ -1,6 +1,7 @@
|
|||||||
package DataStructures.Stacks;
|
package DataStructures.Stacks;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.EmptyStackException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements a Stack using an ArrayList.
|
* This class implements a Stack using an ArrayList.
|
||||||
@ -10,43 +11,49 @@ import java.util.ArrayList;
|
|||||||
* <p>
|
* <p>
|
||||||
* This is an ArrayList Implementation of a stack, where size is not
|
* This is an ArrayList Implementation of a stack, where size is not
|
||||||
* a problem we can extend the stack as much as we want.
|
* a problem we can extend the stack as much as we want.
|
||||||
*
|
|
||||||
* @author Unknown
|
|
||||||
*/
|
*/
|
||||||
public class StackArrayList {
|
public class StackArrayList {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main method
|
* Driver Code
|
||||||
*
|
|
||||||
* @param args Command line arguments
|
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
StackArrayList stack = new StackArrayList();
|
||||||
StackArrayList myStackArrayList = new StackArrayList();
|
assert stack.isEmpty();
|
||||||
|
|
||||||
myStackArrayList.push(5);
|
for (int i = 1; i <= 5; ++i) {
|
||||||
myStackArrayList.push(8);
|
stack.push(i);
|
||||||
myStackArrayList.push(2);
|
assert stack.size() == i;
|
||||||
myStackArrayList.push(9);
|
}
|
||||||
|
|
||||||
|
assert stack.size() == 5;
|
||||||
|
assert stack.peek() == 5 && stack.pop() == 5 && stack.peek() == 4;
|
||||||
|
|
||||||
|
/* pop elements at the top of this stack one by one */
|
||||||
|
while (!stack.isEmpty()) {
|
||||||
|
stack.pop();
|
||||||
|
}
|
||||||
|
assert stack.isEmpty();
|
||||||
|
|
||||||
|
try {
|
||||||
|
stack.pop();
|
||||||
|
assert false; /* this should not happen */
|
||||||
|
} catch (EmptyStackException e) {
|
||||||
|
assert true; /* this should happen */
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("*********************Stack List Implementation*********************");
|
|
||||||
System.out.println(myStackArrayList.isEmpty()); // will print false
|
|
||||||
System.out.println(myStackArrayList.peek()); // will print 9
|
|
||||||
System.out.println(myStackArrayList.pop()); // will print 9
|
|
||||||
System.out.println(myStackArrayList.peek()); // will print 2
|
|
||||||
System.out.println(myStackArrayList.pop()); // will print 2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ArrayList representation of the stack
|
* ArrayList representation of the stack
|
||||||
*/
|
*/
|
||||||
private ArrayList<Integer> stackList;
|
private ArrayList<Integer> stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public StackArrayList() {
|
public StackArrayList() {
|
||||||
stackList = new ArrayList<>();
|
stack = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,42 +63,51 @@ public class StackArrayList {
|
|||||||
* @param value value to be added
|
* @param value value to be added
|
||||||
*/
|
*/
|
||||||
public void push(int value) {
|
public void push(int value) {
|
||||||
stackList.add(value);
|
stack.add(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pops last element of list which is indeed
|
* Removes the element at the top of this stack and returns
|
||||||
* the top for Stack
|
|
||||||
*
|
*
|
||||||
* @return Element popped
|
* @return Element popped
|
||||||
|
* @throws EmptyStackException if the stack is empty.
|
||||||
*/
|
*/
|
||||||
public int pop() {
|
public int pop() {
|
||||||
|
if (isEmpty()) {
|
||||||
if (!isEmpty()) { // checks for an empty Stack
|
throw new EmptyStackException();
|
||||||
int popValue = stackList.get(stackList.size() - 1);
|
|
||||||
stackList.remove(stackList.size() - 1); // removes the poped element from the list
|
|
||||||
return popValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.print("The stack is already empty!");
|
/* remove the element on the top of the stack */
|
||||||
return -1;
|
return stack.remove(stack.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks for empty Stack
|
* Test if the stack is empty.
|
||||||
*
|
*
|
||||||
* @return true if stack is empty
|
* @return {@code true} if this stack is empty, {@code false} otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return stackList.isEmpty();
|
return stack.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Top element of stack
|
* Return the element at the top of this stack without removing it from the stack.
|
||||||
*
|
*
|
||||||
* @return top element of stack
|
* @return the element at the top of this stack.
|
||||||
*/
|
*/
|
||||||
public int peek() {
|
public int peek() {
|
||||||
return stackList.get(stackList.size() - 1);
|
if (isEmpty()) {
|
||||||
|
throw new EmptyStackException();
|
||||||
|
}
|
||||||
|
return stack.get(stack.size() - 1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Return size of this stack.
|
||||||
|
*
|
||||||
|
* @return size of this stack.
|
||||||
|
*/
|
||||||
|
public int size() {
|
||||||
|
return stack.size();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user