Monday, 15 June 2020

Stack Using Array - Coursera Algorithms

Stack works in a LIFO order. It supports insert, remove, iteration and test for empty operations.

They can be represented using array. An initial capacity for this array is taken as input. We manipulate an index variable for push and pop, instead of actually deleting items from stack.

If using objects in stack, avoid loitering(holding references to objects and preventing GC) by using following code in pop operation:

public String pop() { String item = s[--N]; s[N] = null; return item; } 

Following is the complete program:
package stacks;
import stacks.StackLL.Node;
public class StackA {
int[] stack;
int index=0;
public StackA(int cap) {
stack = new int[cap];
}
public void push(int v) {
stack[index++] = v;
}
public int pop() {
if(isEmpty()) {
System.out.println("stack empty");
return Integer.MIN_VALUE;
}
return stack[--index];
}
public boolean isEmpty() {
return (index==0);
}
public int size() {
return index;
}
public void print() {
System.out.println("============================================");
for(int j=index-1;j>=0;j--) {
System.out.println(stack[j]);
}
}
public static void main(String[] args) {
StackA stack = new StackA(10);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.print();
stack.pop();
stack.pop();
stack.print();
System.out.println("size is "+stack.size());
stack.pop();
stack.pop();
stack.pop();
stack.pop();
}
}
view raw StackA.java hosted with ❤ by GitHub

Related Posts:  

Stack Using Linked List - Coursera Algorithms

No comments:

Post a Comment