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:
Java Program to Find a Good Feedback Vertex Set
Summing Numbers with Java Streams
Java Program to Implement Miller Rabin Primality Test Algorithm
Java Program to Generate Random Numbers Using Middle Square Method
Allow user:password in URL
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Spring Boot - Interceptor
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
An Intro to Spring Cloud Task
Java Program to Implement LinkedBlockingDeque API
Comparing Objects in Java
Java Program to Find the Mode in a Data Set
Giới thiệu thư viện Apache Commons Chain
Login For a Spring Web App – Error Handling and Localization
Explain about URL and HTTPS protocol
Java Program to Implement TreeSet API
New Stream Collectors in Java 9
Java Program to Perform the Sorting Using Counting Sort
ExecutorService – Waiting for Threads to Finish
Java Program to Implement Hash Tables Chaining with Binary Trees
Spring Data MongoDB – Indexes, Annotations and Converters
Spring Boot - Scheduling
Introduction to Using FreeMarker in Spring MVC
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Prevent Cross-Site Scripting (XSS) in a Spring Application
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Case-Insensitive String Matching in Java
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Spring @Primary Annotation
Java Program to Check whether Directed Graph is Connected using DFS