Data Structures : Stack

Stack is the data structure that can be logically through as Linear Data Structure. The basic implementation is called as LIFO ( Last In First Out ) way.




 







There are three operation can be perform in a stack data structure. 
  1. Push ( Insertion of an element )
  2. Pop ( Deletion and return of top element )
  3. Peek (Return of top element, just pick and see what it is.)
Before going to the implementation lets look a Demo by Y. Daniel Liang.
http://www.cs.armstrong.edu/liang/animation/web/Stack.html
 

package com.nifrasismail.Stack;
public class MyStack {
private int size;
private int[] StackHolder;
private int top;
public MyStack(int size) {
this.size = size;
StackHolder = new int[size];
top = -1;
}
public void push(int element) {
if (!isFull()) {
top++;
StackHolder[top] = element;
}
}
public int peek() {
return StackHolder[top];
}
public int pop() {
return StackHolder[top--];
}
public Boolean isEmpty() {
return (top == -1);
}
public Boolean isFull() {
return (top == size - 1);
}
}
view raw MyStack.java hosted with ❤ by GitHub
package com.nifrasismail.Stack;
public class UseStack {
public static void main(String[] args){
MyStack S = new MyStack(20); //[] top=-1
S.push(25); // [20] top =0
S.push(35); // [20,35] top=1
S.push(22); // [20,35,22] top=2
System.out.println("Current Peek Value is : "+S.peek());
S.push(99); // [20,35,22,99] top=3
S.push(82); // [20,35,22,99,82] top=4
S.pop(); // [20,35,22,99] top=3
S.pop(); // [20,35,22,99] top=2
while(!S.isEmpty()){
System.out.println("Poped Element "+ S.pop());
}
}
}
view raw UseStack.java hosted with ❤ by GitHub

Comments

Popular posts from this blog

Missionaries & Canibal Problem in AI using Pro Log

Spring Boot - No Need to Restart the Development Server for Each Time