In computer science, a stack is a data structure that follows the Last In First Out (LIFO) principle.
It is a collection of elements that can be added and removed, with the most recently added element being the first one to be removed.
Stacks are used in various algorithms, such as parsing expressions, reversing strings, and implementing recursive functions.
In this tutorial, we will see how to implement a stack data structure in Java.
To implement a stack, we can use the built-in Java class java.util.Stack.
This class provides methods for push, pop, peek, search, and other stack operations.
Here is an example of how to create and use a stack:
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack); // [1, 2, 3]
System.out.println(stack.pop()); // 3
System.out.println(stack.peek()); // 2
System.out.println(stack.search(1)); // 2
}
}In this example, we first create a new Stack object with the data type Integer.
We then push three integers onto the stack using the push method.
The println statements print the contents of the stack, the result of popping the top element, the result of peeking at the top element, and the result of searching for an element in the stack.
We can also implement a stack using an array or a linked list.
Here is an example of how to implement a stack using an array:
public class ArrayStack {
private int[] stack;
private int top;
public ArrayStack(int capacity) {
stack = new int[capacity];
top = -1;
}
public void push(int item) {
if (top == stack.length - 1) {
throw new RuntimeException("Stack is full");
}
top++;
stack[top] = item;
}
public int pop() {
if (top == -1) {
throw new RuntimeException("Stack is empty");
}
int item = stack[top];
top--;
return item;
}
public int peek() {
if (top == -1) {
throw new RuntimeException("Stack is empty");
}
return stack[top];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == stack.length - 1;
}
}In this example, we first define a private integer array stack to hold the elements of the stack, and an integer variable top to keep track of the top element.
We then define a constructor that takes a capacity argument and initializes the stack and top variables.
The push method adds an item to the top of the stack, the pop method removes the top item and returns it, the peek method returns the top item without removing it, and the isEmpty and isFull methods check whether the stack is empty or full, respectively.
To use this implementation of a stack, we can create a new ArrayStack object and call its methods, as shown in the following example:
public class ArrayStackExample {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(3);
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.pop()); // 3
System.out.println(stack.peek()); // 2
System.println(stack.isEmpty()); // false System.out.println(stack.isFull()); // true } }In this example, we first create a new ArrayStack object with a capacity of 3.
We then push three integers onto the stack using the push method.
The println statements print the result of popping the top element, peeking at the top element, and checking whether the stack is empty or full.
In conclusion, stacks are a fundamental data structure used in many programming applications.
In Java, we can implement a stack using the built-in java.util.Stack class or by using an array or a linked list.
Both implementations have their advantages and disadvantages, depending on the specific use case.
It is important to choose the appropriate data structure for each problem to ensure optimal performance and efficiency.




