Adding ArrayStack Implementation

This commit is contained in:
hitanshu310 2025-01-26 21:24:16 +05:30
parent 9298520865
commit a1c1b2a0f4
3 changed files with 116 additions and 1 deletions

View File

@ -0,0 +1,19 @@
package com.hithomelabs.clients;
import com.hithomelabs.princeton1.module4.ArrayStack;
import com.hithomelabs.princeton1.module4.Stack;
public class ArrayStackClient {
public static void main(String[] args) {
Stack<Integer> stack = new ArrayStack<Integer>();
for(int i = 0; i < 21; i++){
stack.push(i);
}
// Using object s, as stack is generic but every java Object has implementation of toString()
for (Object s : stack)
System.out.println(s);
while(!stack.isEmpty()){
System.out.println(stack.pop());
}
}
}

View File

@ -4,7 +4,7 @@ package com.hithomelabs.clients;
import com.hithomelabs.princeton1.module4.LinkedStack;
import com.hithomelabs.princeton1.module4.Stack;
public class Main {
public class LinkedArrayClient {
public static void main(String[] args) {
Stack<String> stack = new LinkedStack<String>();
stack.push("hello");

View File

@ -0,0 +1,96 @@
package com.hithomelabs.princeton1.module4;
import java.util.Iterator;
// Concrete implementation of stack using arrays
// Creating a generic stack of type E
public class ArrayStack<E> extends Stack<E> {
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10
// Everytime size == capacity, capacity = 2 * capacity
private static final int DEFAULT_CAPACITY = 10;
private int capacity;
private int size;
private E[] arr;
public ArrayStack(int capacity){
this.capacity = capacity;
this.arr = (E[]) new Object[this.capacity];
}
// Constructor chaining, default constructor will call parametrised constructor with default initial capacity 10
public ArrayStack(){
this(DEFAULT_CAPACITY);
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
private void changeCapacity(int newCapacity){
E[] resizedArr = (E[]) new Object[newCapacity];
for (int i = 0; i < this.size; i++)
resizedArr[i] = this.arr[i];
this.arr = resizedArr;
}
private void incrementSize(){
if (this.size == this.capacity){
this.capacity = 2 * this.capacity;
changeCapacity(this.capacity);
}
}
// Push always happens at the end of the stack
// Say the size of the stack is 1, new element gets inserted at 1
@Override
public void push(E element) {
// Lazy approach, we assume size to always be lesser than capacity
this.incrementSize();
this.arr[this.size++] = element;
}
@Override
public E pop() {
if (isEmpty())
return null;
else{
E e = this.arr[--this.size];
this.arr[this.size] = null;
this.checkResize();
return e;
}
}
private void checkResize() {
if (this.size < this.capacity / 4 && this.capacity > 10){
this.capacity = this.capacity / 2;
changeCapacity(this.capacity);
}
}
@Override
public int size() {
return this.size;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
int current = 0;
@Override
public boolean hasNext() {
return current != size;
}
@Override
public E next() {
E element = arr[current];
current = current + 1;
return element;
}
};
}
}