Зміст курсу
Advanced Java 2077
Advanced Java 2077
Stacks
A stack is a collection of elements with two main operations. Those operations are push and pop.
Push adds an element to the collection, and pop removes the most recently added element. The order in which elements are added or removed is known as last-in, first-out (LIFO).
In Java, stacks can be implemented using the built-in Stack
class or by using the Deque interface with the LinkedList
class. In this chapter, we will explore both implementations of stacks in Java and learn how to use them in our programs.
Implementation of Stacks using Stack Class
The Stack
class in Java is a subclass of the Vector
class, which is a resizable array. It provides the two main operations of a stack: push and pop. Here is an example of how to use the Stack
class.
Main
// This package is employed to run code on Codefinity. package com.example; // It is not required to be written in typical IDEs. import java.util.Stack; public class StackExample { public static void main(String[] args) { Stack<String> stack = new Stack<>(); stack.push("apple"); stack.push("banana"); stack.push("orange"); System.out.println("Stack elements: " + stack); String popped = stack.pop(); System.out.println("Popped element: " + popped); System.out.println("Stack elements after pop operation: " + stack); String peeked = stack.peek(); System.out.println("Peeked element: " + peeked); System.out.println("Stack elements after peek operation: " + stack); } }
The push()
method adds an element to the top of the stack, and the pop()
method removes the top element from the stack. The peek()
method returns the top element of the stack without removing it. The empty()
method checks if the stack is empty or not, and the search()
method returns the 1-based position of the element from the top of the stack.
Implementation of Stacks using Deque Interface and LinkedList Class
Another way to implement stacks in Java is by using the Deque interface with the LinkedList
class. The Deque interface provides the same functionality as the Stack
class, but with additional methods such as addFirst()
, addLast()
, removeFirst()
, and removeLast()
.
Here is an example of how to use the Deque interface with the LinkedList
class.
Main
// This package is employed to run code on Codefinity. package com.example; // It is not required to be written in typical IDEs. import java.util.Deque; import java.util.LinkedList; public class DequeExample { public static void main(String[] args) { Deque<String> deque = new LinkedList<>(); deque.addFirst("apple"); deque.addLast("banana"); deque.addFirst("orange"); System.out.println("Deque elements: " + deque); String removedFirst = deque.removeFirst(); System.out.println("Removed first element: " + removedFirst); System.out.println("Deque elements after removeFirst operation: " + deque); String removedLast = deque.removeLast(); System.out.println("Removed last element: " + removedLast); System.out.println("Deque elements after removeLast operation: " + deque); } }
The addFirst()
method adds an element to the beginning of the deque, and the addLast()
method adds an element to the end of the deque. The removeFirst()
method removes and returns the first element of the deque, while the removeLast()
method removes and returns the last element of the deque.
Дякуємо за ваш відгук!