Stack
is a linear data structure ( an abstract data type ) with a fixed capacity. Linear data structure which is simple to use and allows adding and removing elements in a particular order i.e. a last-in-first-out (LIFO)
order. Every element enters the stack from the top and leaves from the top.
Example: Just like a stack of books where we can only put the a new book on top of other book and pick any book from the top only. This order is called last-in-first-out (LIFO)
In stack, elements are stored and accessed in Last In First Out manner. That is, elements are added to the top of the stack and removed from the top of the stack.
Stack
extends class Vector
with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.
Create a Stack :
To create a stack, we import the java.util.Stack
package. After we import the package, we can create stacks as follows.
Stack<type> stacks = new Stack<>();
Here, type
indicates the stack’s type. For example,
// Create String type stack
Stack<String> stacks = new Stack<>();
// Create Integer type stack
Stack<Integer> stacks = new Stack<>();
Methods :
Since Stack
extends the Vector
class, it inherits all the methods Vector
. Apart from these methods, the Stack
class includes 5 more methods that differentiates it from Vector
.
push() Method
To add an element to the top of the stack, we use the push()
method. For example,
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<String> foodName= new Stack<>();
// Add elements to Stack
foodName.push("Apple");
foodName.push("Mango");
foodName.push("Banana");
System.out.println("Food Name: " + foodName);
}
}
Output
Food Name: [Apple, Mango, Banana]
pop() Method
To remove an element from the top of the stack, we use the pop()
method. For example,
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<String> foodName= new Stack<>();
// Add elements to Stack
foodName.push("Apple");
foodName.push("Mango");
foodName.push("Banana");
System.out.println("Food Name: " + foodName);
// Remove element stacks
String element = foodName.pop();
System.out.println("Removed food: " + element);
}
}
Output
Food Name: [Apple, Mango, Banana]
Removed food: Banana
peek() Method
The peek()
method returns an object from the top of the stack. For example,
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<String> foodName= new Stack<>();
// Add elements to Stack
foodName.push("Apple");
foodName.push("Mango");
foodName.push("Banana");
System.out.println("Food Name: " + foodName);
// Access element from the top
String element = foodName.peek();
System.out.println("Food at top: " + element);
}
}
Output
Food Name: [Apple, Mango, Banana]
Food at top: Banana
search() Method
To search an element in the stack, we use the search()
method. It returns the position of the element from the top of the stack. For example,
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<String> foodName= new Stack<>();
// Add elements to Stack
foodName.push("Apple");
foodName.push("Mango");
foodName.push("Banana");
System.out.println("Food Name: " + foodName);
// Search an element
int position = foodName.search("Mango");
System.out.println("Position of Horse: " + position);
}
}
Output
Food Name: [Apple, Mango, Banana]
Position of Mango: 2
empty() Method
To check whether a stack is empty or not, we use the empty()
method. For example,
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<String> foodName= new Stack<>();
// Add elements to Stack
foodName.push("Apple");
foodName.push("Mango");
foodName.push("Banana");
System.out.println("Food Name: " + foodName);
// Check if stack is empty
boolean result = foodName.empty();
System.out.println("Is the stack empty? " + result);
}
}
Output
Food Name: [Apple, Mango, Banana]
Is the stack empty? false
Pros: The linked list implementation of stack can grow and shrink according to the needs at runtime.
Cons: Requires extra memory due to involvement of pointers.