This Java program Implements Stack API.The Stack class represents a last-in-first-out (LIFO) stack of objects. It 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.
Here is the source code of the Java Program to Implement Stack API.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
import java.util.Stack; public class StackImpl<E> { private Stack<E> stack; /** Creates an empty Stack. **/ public StackImpl() { stack = new Stack<E>(); } /** Tests if this stack is empty. **/ public boolean empty() { return stack.empty(); } /** * Looks at the object at the top of this stack without removing it from the * stack. **/ public E peek() { return stack.peek(); } /** * Removes the object at the top of this stack and returns that object as * the value of this function. **/ public E pop() { return stack.pop(); } /** Pushes an item onto the top of this stack. **/ public E push(E item) { return stack.push(item); } /** Returns the 1-based position where an object is on this stack. **/ public int search(Object o) { return stack.search(o); } public static void main(String...arg) { StackImpl<Integer> stack = new StackImpl<Integer>(); System.out.println("element pushed : " + stack.push(3)); System.out.println("element pushed : " + stack.push(4)); System.out.println("element pushed : " + stack.push(-19)); System.out.println("element pushed : " + stack.push(349)); System.out.println("element pushed : " + stack.push(35)); System.out.println("element poped : " + stack.pop()); System.out.println("element poped : " + stack.pop()); System.out.println("Element peek : " + stack.peek()); System.out.println("position of element 349 " + stack.search(3)); while (!stack.empty()) { System.out.println("element poped : " + stack.pop()); } } }
$ javac StackImpl.java $ java StackImpl element pushed : 3 element pushed : 4 element pushed : -19 element pushed : 349 element pushed : 35 element poped : 35 element poped : 349 Element peek : -19 position of element 349 3 element poped : -19 element poped : 4 element poped : 3
Related posts:
Simultaneous Spring WebClient Calls
Spring Data JPA and Null Parameters
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java – InputStream to Reader
Guide to Java OutputStream
An Intro to Spring Cloud Security
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Implement Heap
Java Program to Implement Efficient O(log n) Fibonacci generator
Spring Boot - Introduction
Stack Memory and Heap Space in Java
Simplify the DAO with Spring and Java Generics
The XOR Operator in Java
Java Program to Implement Queue using Two Stacks
Cơ chế Upcasting và Downcasting trong java
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Add Multiple Items to an Java ArrayList
What is Thread-Safety and How to Achieve it?
Spring Boot - Enabling HTTPS
Java Program to Implement ConcurrentLinkedQueue API
Period and Duration in Java
An Intro to Spring Cloud Zookeeper
Java Program to Create a Random Graph Using Random Edge Generation
Logout in an OAuth Secured Application
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
LinkedHashSet trong java
Spring Boot - Sending Email
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Model, ModelMap, and ModelAndView in Spring MVC