0% found this document useful (0 votes)
7 views2 pages

Stack

Uploaded by

tolaso7724
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Stack

Uploaded by

tolaso7724
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

public class Stack {

int[] arr;
int top;
int capacity;
public Stack(int size){
arr = new int[size];
top = -1;
capacity = size;
}

//check stack is empty or not


public boolean isEmpty(){
return top == -1;
}

//check stack is full or not


public boolean isFull(){
return top == capacity-1;
}

//push element
public void push(int element){
if(isFull()){
[Link]("Stack is full !!!");
return;
}
arr[++top] = element;
[Link]("element pushed successfully !!!" + element);
}

//pop the element


public int pop(){
if(isEmpty()){
[Link]("Stack is empty !!!");
return -1;
}
else{
return arr[top--];
}
}

//peek operation
public int peek(){
if(isEmpty()){
[Link]("Stack is empty !!!");
return -1;
}else{
return arr[top];
}
}

//display
public void display(){
if(isEmpty()){
[Link]("Stack is empty !!!");
return;
}else{
for(int i=0;i<=top;i++){
[Link](arr[i]);
}
}
}

You might also like