Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions DataStructures/Stacks/StackArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* This class implements a Stack using a regular array.
* <p>
* 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
*/
public class StackArray {

/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String[] args) {
// Declare a stack of maximum size 4
StackArray myStackArray = new StackArray(4);

// Populate the stack
myStackArray.push(5);
myStackArray.push(8);
myStackArray.push(2);
myStackArray.push(9);

System.out.println("*********************Stack Array Implementation*********************");
System.out.println(myStackArray.isEmpty()); // will print false
System.out.println(myStackArray.isFull()); // will print true
System.out.println(myStackArray.peek()); // will print 9
System.out.println(myStackArray.pop()); // will print 9
System.out.println(myStackArray.peek()); // will print 2
}

/**
* The max size of the Stack
*/
private int maxSize;

/**
* The array representation of the Stack
*/
private int[] stackArray;

/**
* The top of the stack
*/
private int top;

/**
* Constructor
*
* @param size Size of the Stack
*/
public StackArray(int size) {
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) {
if (!isFull()) { // Checks for a full stack
top++;
stackArray[top] = value;
} else {
resize(maxSize * 2);
push(value); // don't forget push after resizing
}
}

/**
* Removes the top element of the stack and returns the value you've removed
*
* @return value popped off the Stack
*/
public int pop() {
if (!isEmpty()) { // Checks for an empty stack
return stackArray[top--];
}

if (top < maxSize / 4) {
resize(maxSize / 2);
return pop();// don't forget pop after resizing
} else {
System.out.println("The stack is already empty");
return -1;
}
}

/**
* Returns the element at the top of the stack
*
* @return element at the top of the stack
*/
public int peek() {
if (!isEmpty()) { // Checks for an empty stack
return stackArray[top];
} else {
System.out.println("The stack is empty, cant peek");
return -1;
}
}

private void resize(int newSize) {
// private int[] transferArray = new int[newSize]; we can't put modifiers here !
int[] transferArray = new int[newSize];

// for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
for (int i = 0; i < stackArray.length; i++) {
transferArray[i] = stackArray[i];
stackArray = transferArray;
}
maxSize = newSize;
}

/**
* Returns true if the stack is empty
*
* @return true if the stack is empty
*/
public boolean isEmpty() {
return (top == -1);
}

/**
* Returns true if the stack is full
*
* @return true if the stack is full
*/
public boolean isFull() {
return (top + 1 == maxSize);
}

/**
* Deletes everything in the Stack
* <p>
* Doesn't delete elements in the array
* but if you call push method after calling
* makeEmpty it will overwrite previous
* values
*/
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
}
}
95 changes: 95 additions & 0 deletions DataStructures/Stacks/StackArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.util.ArrayList;

/**
* This class implements a Stack using an ArrayList.
* <p>
* 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.
* <p>
* This is an ArrayList Implementation of a stack, where size is not
* a problem we can extend the stack as much as we want.
*
* @author Unknown
*/
public class StackArrayList {

/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String[] args) {

StackArrayList myStackArrayList = new StackArrayList();

myStackArrayList.push(5);
myStackArrayList.push(8);
myStackArrayList.push(2);
myStackArrayList.push(9);

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
*/
private ArrayList<Integer> stackList;

/**
* Constructor
*/
public StackArrayList() {
stackList = new ArrayList<>();
}

/**
* Adds value to the end of list which
* is the top for stack
*
* @param value value to be added
*/
public void push(int value) {
stackList.add(value);
}

/**
* Pops last element of list which is indeed
* the top for Stack
*
* @return Element popped
*/
public int pop() {

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;
}

System.out.print("The stack is already empty!");
return -1;
}

/**
* Checks for empty Stack
*
* @return true if stack is empty
*/
public boolean isEmpty() {
return stackList.isEmpty();
}

/**
* Top element of stack
*
* @return top element of stack
*/
public int peek() {
return stackList.get(stackList.size() - 1);
}
}
Loading