JavaAlgorithms/data_structures/Stacks.java

208 lines
4.6 KiB
Java
Raw Normal View History

import java.util.ArrayList;
/**
* This class implements a Stack using two different implementations.
* Stack is used with a regular array and Stack2 uses an ArrayList.
*
* A stack is exactly what it sounds like. An element gets added to the top of
* the stack and only the element on the top may be removed. This is an example
* 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
* array implementation it does.
*
* @author Unknown
*
*/
2016-11-21 20:38:53 +08:00
class Stack{
/** The max size of the Stack */
2016-11-21 20:38:53 +08:00
private int maxSize;
/** The array representation of the Stack */
2016-11-21 20:38:53 +08:00
private int[] stackArray;
/** The top of the stack */
2016-11-21 20:38:53 +08:00
private int top;
/**
* Constructor
*
* @param size Size of the Stack
*/
public Stack(int size){
2016-11-21 20:38:53 +08:00
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
/**
* Adds an element to the top of the stack
*
* @param value The element added
*/
public void push(int value){
2016-11-21 20:38:53 +08:00
top++;
stackArray[top] = value;
}
/**
* Removes the top element of the stack and returns the value you've removed
*
* @return value popped off the Stack
*/
public int pop(){
2016-11-21 20:38:53 +08:00
return stackArray[top--];
}
/**
* Returns the element at the top of the stack
*
* @return element at the top of the stack
*/
public int peek(){
2016-11-21 20:38:53 +08:00
return stackArray[top];
}
/**
* Returns true if the stack is empty
*
* @return true if the stack is empty
*/
public boolean isEmpty(){
2016-11-21 20:38:53 +08:00
return(top == -1);
}
/**
* Returns true if the stack is full
*
* @return true if the stack is full
*/
public boolean isFull(){
2016-11-21 20:38:53 +08:00
return(top+1 == maxSize);
}
/**
* Deletes everything in the Stack
*
* Doesn't delete elements in the array
* but if you call push method after calling
* makeEmpty it will overwrite previous
* values
*/
2016-11-21 20:38:53 +08:00
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
}
}
2017-03-10 03:11:42 +08:00
/**
* This is an ArrayList Implementation of stack, Where size is not
* a problem we can extend the stack as much as we want.
*
* @author Unknown
*
*/
2017-03-10 03:11:42 +08:00
class Stack2{
/** ArrayList representation of the stack */
2017-03-10 03:11:42 +08:00
ArrayList<Integer> stackList;
/**
* Constructor
*/
Stack2(){
2017-03-10 03:11:42 +08:00
stackList=new ArrayList<>();
}
/**
* Adds value to the end of list which
* is the top for stack
*
* @param value value to be added
*/
void push(int value){
2017-03-10 03:11:42 +08:00
stackList.add(value);
}
/**
* Pops last element of list which is indeed
* the top for Stack
*
* @return Element popped
*/
int pop(){
2017-03-10 03:11:42 +08:00
if(!isEmpty()){ // checks for an empty Stack
int popValue=stackList.get(stackList.size()-1);
stackList.remove(stackList.size()-1); //removes the poped element from the list
return popValue;
}
else{
System.out.print("The stack is already empty ");
return -1;
}
}
/**
* Checks for empty Stack
*
* @return true if stack is empty
*/
boolean isEmpty(){
2017-03-10 03:11:42 +08:00
if(stackList.isEmpty())
return true;
else return false;
}
/**
* Top element of stack
*
* @return top element of stack
*/
int peek(){
2017-03-10 03:11:42 +08:00
return stackList.get(stackList.size()-1);
}
}
/**
* This class implements the Stack and Stack2 created above
*
* @author Unknown
*
*/
2016-11-21 20:38:53 +08:00
public class Stacks{
/**
* Main method
*
* @param args Command line arguments
*/
2016-11-21 20:38:53 +08:00
public static void main(String args[]){
2017-03-10 03:11:42 +08:00
Stack myStack = new Stack(4); //Declare a stack of maximum size 4
//Populate the stack
myStack.push(5);
myStack.push(8);
myStack.push(2);
myStack.push(9);
System.out.println("*********************Stack Array Implementation*********************");
System.out.println(myStack.isEmpty()); //will print false
System.out.println(myStack.isFull()); //will print true
System.out.println(myStack.peek()); //will print 9
System.out.println(myStack.pop()); //will print 9
System.out.println(myStack.peek()); // will print 2
Stack2 myStack2 = new Stack2(); //Declare a stack of maximum size 4
//Populate the stack
myStack2.push(5);
myStack2.push(8);
myStack2.push(2);
myStack2.push(9);
System.out.println("*********************Stack List Implementation*********************");
System.out.println(myStack2.isEmpty()); //will print false
System.out.println(myStack2.peek()); //will print 9
System.out.println(myStack2.pop()); //will print 9
System.out.println(myStack2.peek()); // will print 2
System.out.println(myStack2.pop()); //will print 2
}
}