Stack Data Structure
Stack have Last in First out(LIFO) Characteristic. It is Container which stores orderly the similar type of objects. When we delete object from the stack then it removes last object that was inserted in the stack.
Push & Pop
- Push: Push means adding something to the Stack.
- Pop: Removing from the Stack it's called Pop.
Applications of Stack
- Undo and Redo: One of the Finest example of Stack Data Structure. When we do change in microsoft word document, word PUSH that change to the stack. When we need to remove that change we simply click on redo option so word perform POP operation on the stack and bring the change.
- In Recursion: When function calls itself it's call Recursion. In Recursion also Stack Data Structure is used to manage function calls.
- Expression Evaluation: Stack is used to check opening and closing brackets are in pair or not that's how we can evaluate expression.
Stack Class in Java
Java has Collections framework, Stack class is part of this framework. It extends Vector Class for it's fuctionality.Implemetation of Stack
//Create Stack class object
Stack<Type> stack = new Stack<>();//Create Stack of Integer
Methods of Stack Class
push()
push() used with stack object to add data into the stack.import java.util.*;
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
System.out.println("Home Corentine: " + homeStack);
}
}
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
System.out.println("Home Corentine: " + homeStack);
}
}
Output
Home Corentine: [member 1, member 2, member 3]
pop()
pop() used with stack object to remove data from the stack.import java.util.*;
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
homeStack.pop();
homeStack.pop();
System.out.println("Remain Home Corentine: " + homeStack);
}
}
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
homeStack.pop();
homeStack.pop();
System.out.println("Remain Home Corentine: " + homeStack);
}
}
Output
Remain Home Corentine: [member 1]
peek()
peek() used to retrieve top object from the stack.import java.util.*;
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
String lastHomeCorentine = homeStack.peek();
System.out.println("Last Home Corentine: " + lastHomeCorentine);
}
}
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
String lastHomeCorentine = homeStack.peek();
System.out.println("Last Home Corentine: " + lastHomeCorentine);
}
}
Output
Last Home Corentine: [member 3]
search()
search() method returns the position of the elementimport java.util.*;
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
int positionOfMember = homeStack.search("member 2");
System.out.println("Posotion of member 2: " + positionOfMember);
}
}
class StackExample{
public static void main(String... arg){
Stack<String> homeStack = new Stack<>();
homeStack.push("member 1");
homeStack.push("member 2");
homeStack.push("member 3");
int positionOfMember = homeStack.search("member 2");
System.out.println("Posotion of member 2: " + positionOfMember);
}
}
Output
Posotion of member 2: 2
May 20, 2020
Tags :
Data Structure
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
Please comment here...