Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack implementations and notes #94

Merged
merged 1 commit into from
Feb 10, 2025
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
44 changes: 44 additions & 0 deletions src/Data Structures/Stack/main/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main;

import main.stack.Stack;

public class Main {
public static void main(String[] args) throws Exception {
Stack s = new Stack(5);
s.push(2);

System.out.println(s.showStackContent());

s.push(3);

System.out.println(s.showStackContent());

s.push(4);

System.out.println(s.showStackContent());

s.push(5);

System.out.println(s.showStackContent());

s.push(6);

System.out.println(s.isFull());

// s.push(7);

// s.pop();
s.pop();
s.pop();
s.pop();
s.pop();
s.pop();

System.out.println(s.peek());

System.out.println(s.showStackContent());
System.out.println(s.isEmpty());

// s.pop();
}
}
14 changes: 14 additions & 0 deletions src/Data Structures/Stack/main/exceptions/EmptyStackException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main.exceptions;

public class EmptyStackException extends Exception{
private String message;

public EmptyStackException() {
this.message = "Stack is empty!";
}

public String getMessage() {
return message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main.exceptions;

public class StackOverflowException extends Exception {
private String message;

public StackOverflowException() {
message = "Stack overflow has occurred, max capacity is reached!";
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main.exceptions;

public class StackUnderflowException extends Exception{
private String message;

public StackUnderflowException() {
this.message = "You can't pop from an empty stack!";
}

public String getMessage() {
return message;
}

}
77 changes: 77 additions & 0 deletions src/Data Structures/Stack/main/stack/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main.stack;

import java.util.LinkedList;
import java.util.List;

import main.exceptions.EmptyStackException;
import main.exceptions.StackOverflowException;
import main.exceptions.StackUnderflowException;

public class Stack {
private List<Integer> stackContent;
private int usedCapacity;
private int maxCapacity;

public Stack(int size) {
this.stackContent = new LinkedList<Integer>();
this.usedCapacity = 0;
this.maxCapacity = size;
}

public void push(Integer val) throws Exception {
if (this.maxCapacity > this.usedCapacity) {
this.stackContent.add(val);
this.usedCapacity += 1;
} else {
throw new StackOverflowException();
}
}

public Integer pop() throws StackUnderflowException {
Integer popped = -1;
if (this.usedCapacity <= 0) {
throw new StackUnderflowException();
} else {
Integer elementForRemoval = this.stackContent.get(usedCapacity - 1);
if (elementForRemoval != null) {
popped = elementForRemoval;
this.stackContent.remove(usedCapacity - 1);
this.usedCapacity -= 1;
}
}

return popped;
}

public boolean isFull() {
System.out.println(usedCapacity + "vs" + maxCapacity);
return this.usedCapacity == this.maxCapacity;
}

public boolean isEmpty() {
return this.usedCapacity == 0;
}

public String showStackContent() throws EmptyStackException {
StringBuilder sb = new StringBuilder();
if (!this.isEmpty()) {
sb.append("Bottom of the stack --> |");
for (int i = 0; i < this.usedCapacity; i++) {
sb.append(stackContent.get(i).toString() + "|");
}
sb.append(" <-- Top of the stack");
} else {
throw new EmptyStackException();
}

return sb.toString();
}

public int peek() throws EmptyStackException {
if (!this.isEmpty()) {
return this.stackContent.get(this.usedCapacity - 1);
} else {
throw new EmptyStackException();
}
}
}
35 changes: 35 additions & 0 deletions src/Data Structures/Stack/stack.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--- STACK ---
- A Stack is a linear data structure that follows the "LIFO" prinicple, in other words Last In First Out.
That means that the element that was first added to the stack will be the last element removed, and also
that the last element added to the stack will be the first element removed.
- Stacks can be easily and efficiently implemented using a Linked list data structure because of the time
efficiency of removing from the end of the list and adding to it.
- When initializing a stack we will specify the capacity it has.
- Stack operations:
- push(type val), stacks push operation will add an element that is passed as an argument to the method
to the top of the stack, time efficiency of this operation is O(1)/constant (When an operation is done
in constant time that means that the time needed to execute an operation isn't depending on the size of
the stack itself.). If we tried to push an element to a stack that has reached full capacity we will
get a StackOverflow Exception.
- pop(), if the stack is not empty it will remove the element from the top and also return it to the
caller of the method. If by any means a pop method is callled on a empty stack, a StackUnderflow Exception
will be thrown. This method is also done in constant time!
- peek(), another constant time stack operation that will return the element from the top of the
stack but won't remove it as it's done by the pop operation.
- isEmpty(), stack method that will check if the stack content is empty, and all of the stack capacity
is available. If that is the case it will return a boolean value of true, in any other case it will
return false. This method is implemented so that it's executed in constant time since we are tracking
the usage of the capacity by modifying it after every pop operation where we will decrement the
usedCapacity variable, and after every push operation where we will increment the previously mentioned
variable.
- isFull(), this method will check if the stacks maximum capacity has been reached, if that is the case
a boolean value of true will be return, in any other case a false value will be returned.
- Nice to know is that there is a special type of stack that is called a monotonic stack. A monotonic stack is
a stack that is always organized in a specific order (increasingly or decreasingly). Below you will have a simple
example of a monotonic stack.

--- Increasing Monotonic Stack ---
Bottom -> [1, 2, 3, 4, 5, 6, 8, 9, 100] <- Top

--- Decreasing Monotonic Stack ---
Bottom -> [100, 80, 76, 15, 3, 2, 1, 0] <- Top
Loading