CC-2 : Implement Stack using array.
Description:
Create and test a Stack using array for storage instead of LinkedList.
Test cases and expected outputs:
| Input Parameters |
Expected outputs |
Stack stack=new Stack();
stack.add(9);
stack.add(14);
stack.add(17);
stack.add(34);
stack.add(21);
stack.add(28);
|
Queue nodes: 28 <- 21 <- 34 <- 17 <- 14 <- 9
|
stack.remove();
stack.remove();
stack.remove();
|
Queue nodes: 17 <- 14 <- 9
|
Pseudocode:
| The Array Stack should instantiate and use a fixed size array for storing data elements. The last index of array at which a data element is present also needs to be tracked.
|
| The add() method: If array storage is not full, this method moves all existing elements right by one index and then adds the new element to 0 index of the array. Once the array is full, the add() method does not add the element to the array and returns false.
|
| The remove() method: if storage array is empty, return false, else return the first element of the array and shift all data elements in the array left by 1 index.
|
Code:
public class StackArray {
private int[] stack=new int[10];
private int lastIdx=-1;
public int get() {
if (lastIdx != -1) {
return stack[lastIdx];
}
return Integer.MIN_VALUE;
}
public boolean add(int data){
if (lastIdx == stack.length-1) {return false;}
lastIdx++;
stack[lastIdx]=data;
return true;
}
public int remove() {
if (lastIdx == -1) {return Integer.MIN_VALUE;}
int removedData=stack[lastIdx];
stack[lastIdx]=0;
lastIdx--;
return removedData;
}
}
Click here to download and run code and test cases !
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class StackArray {
private int[] stack=new int[10];
private int lastIdx=-1;
public int get() {
if (lastIdx != -1) {
return stack[lastIdx];
}
return Integer.MIN_VALUE;
}
public boolean add(int data){
if (lastIdx == stack.length-1) {return false;}
lastIdx++;
stack[lastIdx]=data;
return true;
}
public int remove() {
if (lastIdx == -1) {return Integer.MIN_VALUE;}
int removedData=stack[lastIdx];
stack[lastIdx]=0;
lastIdx--;
return removedData;
}
public int getSize() {
return lastIdx+1;
}
public void traverse() {
int sIdx=0;
for (sIdx=0; sIdx < stack.length; sIdx++) {
System.out.print(stack[sIdx]);
if (sIdx < stack.length-1) {System.out.print(",");}
}
}
public void printStack() throws Exception {
System.out.println();
System.out.println("*********************************************************");
System.out.print("Stack Array"+" : ");
int sIdx=0;
for (sIdx=0; sIdx < stack.length; sIdx++) {
System.out.print(stack[sIdx]);
if (sIdx < stack.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*********************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
public static void main(String[] args) {
/***********************
Test Cases given below:
***********************/
try {
StackArray stack=new StackArray();
System.out.print("Add to Stack");
stack.add(9);
stack.printStack();
System.out.print("Add to Stack");
stack.add(14);
stack.printStack();
System.out.print("Add to Stack");
stack.add(17);
stack.printStack();
System.out.print("Add to Stack");
stack.add(34);
stack.printStack();
System.out.print("Add to Stack");
stack.add(21);
stack.printStack();
System.out.print("Add to Stack");
stack.add(28);
stack.printStack();
System.out.println("Stack ARRAY SIZE "+stack.getSize());
System.out.print("\nRemove from Stack");
stack.remove();
stack.printStack();
System.out.print("Remove from Stack");
stack.remove();
stack.printStack();
System.out.print("Remove from Stack");
System.out.println("GET FIRST NODE "+stack.get());
stack.remove();
stack.printStack();
System.out.print("\nRemove from Stack");
stack.remove();
stack.printStack();
System.out.print("Remove from Stack");
stack.remove();
stack.printStack();
System.out.print("Remove from Stack");
stack.remove();
stack.printStack();
System.out.println("Stack ARRAY SIZE "+stack.getSize());
System.out.println("\nGET FIRST NODE "+stack.get());
System.out.print("Add to Stack");
stack.add(99);
stack.printStack();
System.out.print("Add to Stack" + "");
stack.add(44);
stack.printStack();
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
}