stack in java using array - Algomentor

Stack in java all method Push, Pop, Peek, and isEmpty

stack in java using array

Hi, welcome to another session of learning data structure in this particular section we will learn about the stack data structure. we will discuss what is a stack, How it helps us in programming, and why the stack is helpful in solving programming questions. the programming language used by us for this tutorial in java. so let's get started our tutorial stack in java using an array.

What is a stack in java?

The stacks are first in last out (FILO) data structure means the element comes first in the stack will come out last and element come last in the stack will come first so you can say if (LIFO) data structure. You can visualize stack as piles of a plate in the dining hall of a party where each guest comes and pick the top plate. and the person who arranges those plates will also put a new fresh plate at the top. so this is how a stack work.

Pictorial visualization of the stack?

In the upcoming series of images, you see how a stack work in these images we give commend to push and pop to the stack and help you to visualize the stack.

Command 1: Create an empty stack

stack in java example
 Command 2: Push 5 into the stack
stack in java example
 Command 3: Push 10 into the stack

stack in java example
 Command 4: Pop from the stack
stack in java example
 Command 5: Push 5 into the stack
stack in java example
I hove now you clearly understand from the pictorial representation of the stack we will implement all the methods of the stack in java programming and believe me these method are super easy and don't have much code as our singly list in java has.

What are the topic we covered in this article?

  • stack push method in java
  • stack pop method in java
  • stack peek method in java
  • stack empty checking method in java
  • stack search method in java
What is some application of stack?
  • reverse a string.
  • language processing in (Ml) machine learning.
  • use by a browser to maintain Back/Forward.
  • Very much use by recursion.
  • when we press CTRL + Z there is a undo stack who stores our data.


How to create a stack in java using class?

public class stack
{
static int top =-1;
}

creating stack is super easy in java just create a class with name stack and define the static int variable top and initialize is value with -1 and define an array name stack in the main function of java.

How to create a stack push method in java?

public static void push(int[] stack,int data)
{
top++;
stack[top] = data;
}

these are how we create a push method in java we need to increment the value of the top variable by one and put the data in the stack array in top position.

How to create a stack pop method in java?

public static void pop(int[] stack)
{
top--;

}

we just need to decrement the value of the top variable to pop an element in the stack. But the question arises here that element is not removed from the array then how can we say that element is pop out. if you are thinking the same then yes element is not removed from the array but when we use the push method new value is overwritten over the previous value.

How to create a stack peek method in java?

public static void peek(int[] stack)
{
System.out.println("peeking element is "+stack[top]);

}

peek method in the stack is as simple as writing a hello world program we just need to print the top element of the stack.

How to check if the stack is empty or not?

public static boolean empty(int[] stack)
{
if(top==-1)
{
return true;
}
else
{
return false
}
}

This empty method will check if the top is equal to -1 means it's empty otherwise it is not empty in the stack top variable is very much important so always keep top variable updated if we lose our top variable then whole stack program gone loose.

How to check if an element is in the stack?

public static void isHave(int[] stack,int data)
{
boolean flag = false;
for(int i=top;i>-1;i--)
{
if(stack[i]==data)
{
flag = true;
}
}
if(flag == true)
{
System.out.println("element in the stack...");
}
else
{
System.out.println("element not in the stack...");
}
}

This method is a little bit long in comparison to the other stack method but doesn't worry it is easy for this particular method we pass two function argument one is stack and another one is data we need to find it's same as searching an element in an array we will iterate our loop from top to index 0 and if we find our element we will make flag equal to true and after the loop completion, we can check if the flag is true mean element is in the stack otherwise it's not there.

I hope you like a tutorial on stack upcoming we will make a tutorial on more data structure so stay connected with us if you like our tutorial on a stack then share with your coder friends and help them to understand stack more clearly.

If you want full code of stack then Visit My Github link you will find combined code for stack there and get complete inside of the stack.

Thank you very much for reading the article Happy Learning 😀.

if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment

0 Comments