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

Customized stack

{

private int arr[];//Array to store stack elements

private int top; //acts as a cursor for stack

private int capacity;//maximum size of stack

 

//1.constructor to initialize stack

public Customized stack(int size)//size=5

{

arr=new int[size];

capacity=size;//capacity=5

top=-1;

}

//2.method for push operation(inserting data)

public void push(int data)//data=20

{

if(isFull())

{

System.out.println(“stack is full cannot push the elements”);

}

arr[++top]=data;//top=top+1

System.out.println(“Data inserted are=” +data);

}

 

public boolean isFull()

{

return top==capacity-1;//5-1=>4

}

 

 

//3.Removing a top most element(pop operation)

 

public boolean isEmpty()

{

return top==-1;

}

 

public int pop()

{

if(isEmpty())

{

System.out.println(“stack is empty unable to remove elements”);

return -1;

}

return arr[top–];//return top element and going to decrement top

}

 

//method to display

 

public void display()

{

if(isEmpty())

{

System.out.println(“stack is empty unable to display the elements”);

}

System.out.println(“The stack elements are=”);

 

for(int i=0; i<=top; i++)

{

System.out.println(arr[i]+” “);

}

}

 

//method to display the top most element using peek

 

public int peek()

{

if(isEmpty())

{

System.out.println(“The stack is empty unable to display the top most element”);

return -1;

}

return arr[top];

}

}

class Mystacks

{

public static void main(String args[])

{

Customizedstack c=new Customizedstack(5);

 

c.push(10);

c.push(20);

c.push(30);

c.pop();

 

c.display();

 

System.out.println(“Top most element right now=” +c.peek());

}

}

Data structures in java

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