Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Stacks | Data Structures
Advanced Java 2077
course content

Contenido del Curso

Advanced Java 2077

Advanced Java 2077

1. Data Structures
2. Sorting and Searching
3. Concurrent Programming
4. Input-Output (I-O) and Networking
5. Java GUI Development

bookStacks

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.

java

Main

copy
12345678910111213141516171819202122232425
// 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.

java

Main

copy
1234567891011121314151617181920212223242526272829
// 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.

Which method in the Java Stack class is used to remove and return the top element of the stack?

Which method in the Java Stack class is used to remove and return the top element of the stack?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 1
some-alt