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:
OAuth 2.0 Resource Server With Spring Security 5
Converting between an Array and a List in Java
Hướng dẫn Java Design Pattern – Proxy
Java Program to Implement Cartesian Tree
Finding Max/Min of a List or Collection
Introduction to Spring Security Expressions
Jackson Date
Collect a Java Stream to an Immutable Collection
Java Program to Perform Cryptography Using Transposition Technique
Spring Boot - Tracing Micro Service Logs
Java – Try with Resources
A Guide to WatchService in Java NIO2
The Java 8 Stream API Tutorial
Spring Boot - Securing Web Applications
Introduction to Using FreeMarker in Spring MVC
Custom Cascading in Spring Data MongoDB
Java Program to Describe the Representation of Graph using Incidence Matrix
Programmatic Transaction Management in Spring
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Concurrent Test Execution in Spring 5
Java – File to Reader
A Quick Guide to Spring MVC Matrix Variables
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Spring Boot - Cloud Configuration Server
Spring Boot - Interceptor
Java Program to Perform Finite State Automaton based Search
Tính kế thừa (Inheritance) trong java
Java Program to Implement TreeMap API
Java 8 Stream findFirst() vs. findAny()
Java Program to Implement D-ary-Heap
Guide to @ConfigurationProperties in Spring Boot