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.
There are three operation can be perform in a stack data structure.
- Push ( Insertion of an element )
- Pop ( Deletion and return of top element )
- Peek (Return of top element, just pick and see what it is.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
Comments
Post a Comment