Sitemap
Rafia Shaikh | Byte School

Rafia Shaikh | Byte School

Data scientist and teacher passionate about demystifying data. Sharing practical knowledge and insights to help others navigate the world of data.

JAVA — STACK-I

Let’s learn implementation of stack using array(java programming language)

3 min readFeb 1, 2023

--

Press enter or click to view image in full size
Photo by Ryan Fields on Unsplash

Stack is a linear data structure that operates based on the LIFO (Last In First Out) principle, where the last item that was added to the stack is the first to be removed.

The stack performs four main operations: push, pop, peek or top, isEmpty, and search.

· PUSH: Push adds an item to the stack, and if the stack is full, an overflow condition occurs.

· POP: Pop removes an item from the stack in the reverse order in which it was added.

· PEEK: Peek or top returns the top element of the stack,

· isEmpty: while isEmpty returns true if the stack is empty, and false if it is not.

Get Rafia Shaikh | Byte School’s stories in your inbox

Join Medium for free to get updates from this writer.

· SEARCH: The search operation returns the position of an item in the stack if it is present, and returns false if it is not.

Press enter or click to view image in full size

Push method:

//push method
void push(int data){
if(isFull())
System.out.println("Overflow: stack is full");
else{
stack_array[++peek]=data;
System.out.println("Element push "+data);
}
}

Pop method:

//pop method
int pop(){
if(isEmpty()){
System.out.println("Underflow: stack is empty");
return -1;
}
else{
int val=stack_array[peek];
peek--;
return val;
}
}

Peek method:

//peek method
int peek(){
return stack_array[peek];
}

isEmpty method:

//isEmpty method
boolean isEmpty(){
return (peek<=0);
}

isFull method

//isFull method
boolean isFull(){
return (size-1==peek);
}

Search method:

//search method
int search(int data){
if(isEmpty()){
System.out.println("Stack is empty");
return -1;
}

else{
for(int i=0; i<stack_array.length; i++){
if(stack_array[i]==data){
return i;
}
}
return -1;
}
}

Complete Code

//Java complete code
//main class
public class main {
public static void main(String[] args) {
//object of stack class
stack s=new stack(10);
int[] array1={11,12,13,14,15};
for(int i : array1){
s.push(i);
}
System.out.println(s.peek());
System.out.println(s.pop());
System.out.println(s.search(12));

}
}
//stack class
class stack{
int stack_array[];
int peek,size;
// constructor -> initialize the array
stack(int size){
this.size=size;
this.stack_array=new int[size];
this.peek=-1; //peek is negative means stack is empty
}
//push method
void push(int data){
if(isFull())
System.out.println("Overflow: stack is full");
else{
stack_array[++peek]=data;
System.out.println("Element push "+data);
}
}
//pop method
int pop(){
if(isEmpty()){
System.out.println("Underflow: stack is empty");
return -1;
}
else{
int val=stack_array[peek];
peek--;
return val;
}
}
//isEmpty method
boolean isEmpty(){
return (peek<=0);
}
//isFull method
boolean isFull(){
return (size-1==peek);
}
//search method
int search(int data){
if(isEmpty()){
System.out.println("Stack is empty");
return -1;
}

else{
for(int i=0; i<stack_array.length; i++){
if(stack_array[i]==data){
return i;
}
}
return -1;
}
}
int peek(){
return stack_array[peek];
}
}
output of above code
Press enter or click to view image in full size
Photo by MARK ADRIANE on Unsplash

--

--

Rafia Shaikh | Byte School
Rafia Shaikh | Byte School

Written by Rafia Shaikh | Byte School

Data scientist and teacher passionate about demystifying data. Sharing practical knowledge and insights to help others navigate the world of data.