EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Stack:

 

A stack is a linear data structure that follows LIFO(LAST IN FIRST OUT) principle

 

linear data structure–>where elements are arranged in a sequence order. Every elements will be connected to its previous or next element

 

Task Description

 

push(item)———————-Adds an item to the top of the stack

 

pop()—————————Remove and return the top item from the stack

 

peek()————————–Returns the top item without removing it

 

isEmpty()———————–checks if the stack is empty

 

search(obj)——————–searches for an object and returns its position

 

import java.util.*;

class Stackexample

{

public static void main(String args[])

{

Stack<String> s=new Stack<>();

 

//push the elements(inserting elements)

 

s.push(“Apple”);

s.push(“Banana”);

s.push(“cherry”);

 

System.out.println(“the elements are: +s);

 

//peek-display the top element without removing it

 

System.out.println(“Top element is=” +s.peek());

 

//pop-top element will be removed

 

System.out.println(“Top element is=” +s.pop());

 

//check if the stack is empty

 

System.out.println(“whether the stack is empty=” +s.isEmpty());

 

System.out.println(“Top element is=” +s.pop());

System.out.println(“Top element is=” +s.pop());

System.out.println(“the elements are: +s);

}}

Java stack in data structures

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer