Tutorialspoint

April Learning Carnival is here, Use code FEST10 for an extra 10% off

Learning Stack data structure [Java]

person icon Monish Njs

4.1

Learning Stack data structure [Java]

Problem solving using Stacks

updated on icon Updated on Apr, 2024

language icon Language - English

person icon Monish Njs

category icon Development,Software Engineering,Data Structures

Lectures -5

Duration -1 hours

4.1

price-loader

30-days Money-Back Guarantee

Training 5 or more people ?

Get your team access to 10000+ top Tutorials Point courses anytime, anywhere.

Course Description

Stack:

Stack stores elements of the same data type.

We can insert, and remove elements in the stack in the Last In First Out policy (LIFO)

Declaration

Stack<Type> variable_name = new Stack<>();

Stack<Integer> stack = new Stack<>();

Methods used in Stack:

stack_name . push() - insert element into a stack

stack_name . peek() - checks the topmost element in the stack

stack_name . pop() - removes the topmost element in the stack

stack_name . size() - returns the number of elements in the stack

stack_name . empty() - returns true/false. if the stack is empty, it returns true. if the stack is not empty it returns false

stack_name . clear() - clears the stack

stack_name . contains(value) -  returns true if the value we check is there in a stack or else it returns false
stack_name remove(index) - removes the value in the given index

stack_name . search(value) - searches the value in the stack and returns us the index of the value if present.

Iterating stack:

We can go through the stack elements in two ways.

1. Popping the elements

We can pop the stack elements one by one and go through all the elements.

2. For each loop

We can go through the stack by a for loop as below

for(String s : stack_name)

{

//Stack will be iterated from bottom to top direction in this way

}

How to identify if that problem can be solved using a stack

1. when we want to evaluate brackets, and expressions in a certain order we can use stacks.

2. When we want to use backspace characters in the keyboard or in any similar situations, we can use stacks

3. When we want to delete or remove the last elements we can use the stack.

4. When we want to backtrack to something and then again move forward direction multiple times we can use the stack.

5. We can use stack to find some peak elements (assuming the value plotted something like a graph) we can use stacks

Who this course is for:

  • Developers who are learning Data structures and Algorithms

Goals

What will you learn in this course:

  • How to use Stack Data structure
  • When to use Stack data structure
  • How to identify if the problem can be solved using Stack
  • Solving problems in the stack

Prerequisites

What are the prerequisites for this course?

  • Required to know basic Core Java

Learning Stack data structure [Java]

Curriculum

Check out the detailed breakdown of what’s inside the course

Working with Stacks
5 Lectures
  • play icon Introduction to Stacks 10:28 10:28
  • play icon Check if Parentheses are balanced 12:48 12:48
  • play icon Backspace string compare 10:31 10:31
  • play icon Simplify path 13:35 13:35
  • play icon Next greater element 19:12 19:12

Instructor Details

Monish Njs

Monish Njs

e


Course Certificate

Use your certificate to make a career change or to advance in your current career.

sample Tutorialspoint certificate

Our students work
with the Best

Related Video Courses

View More

Annual Membership

Become a valued member of Tutorials Point and enjoy unlimited access to our vast library of top-rated Video Courses

Subscribe now
Annual Membership

Online Certifications

Master prominent technologies at full length and become a valued certified professional.

Explore Now
Online Certifications

Talk to us

1800-202-0515