Total members 11893 |It is currently Fri Nov 15, 2024 7:44 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Java Stack Example (Push/PoP/Peak)

Code:


import java
.awt.Color;

public class 
StackExample {

    public static 
void main(String[] args) {

        
Color c1 = new Color(544375);
        
Color c2 = new Color(4100175);
        
Color c3 = new Color(10053160);
        
JavaStack stack = new JavaStack(2);

        
stack.push(c1);
        
stack.push(c2);
        
stack.push(c3);

        
System.out.println(stack.pop());
        
System.out.println(stack.pop());
        
System.out.println(stack.pop());// Empty
        
stack.push(c3);
        
stack.push(c2);
        
System.out.println(stack.pop());
        
System.out.println(stack.pop());
        
System.out.println(stack.pop());// Empty
        
System.out.println(stack.pop());// Empty


    
}
}
 


Code:
public class JavaStack {

    private 
int top;
    
// Array to store objects
    
private Object[] stackBox;

    
JavaStack(int capacity) {

        
stackBox = new Object[capacity];
        
top = -1;

    }

    
void push(Object value) {

        if (
top+== stackBox.length) {
            
System.out.println("For Value: " value ","
                    
" We can't add more values to the "
                    
" (Stack is full)");
        } else {
            
top++;
            
stackBox[top] = value;
        }

    }

    
Object peek() {

        if (
top == -1) {
            
System.out.println("Stack is empty");
            return 
null;
        } else {
            return 
stackBox[top];
        }

    }

    
Object pop() {

        if (
top == -1) {
            
System.out.println("Stack is empty");
            return 
null;
        } else {
           
// Decreases the value after assigning it stackBox
            
return stackBox[top--];
        }
    }

    
boolean isEmpty() {
         
// True if empty 
        
return (top == -1);

    }


}

 


The output is :
Code:
For Value: java.awt.Color[r=100,g=53,b=160], We can't add more values to the  (Stack is full)
java.awt.Color[r=4,g=100,b=175]
java.awt.Color[r=54,g=43,b=75]
Stack is empty
null
java.awt.Color[r=4,g=100,b=175]
java.awt.Color[r=100,g=53,b=160]
Stack is empty
null
Stack is empty
null




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Stack using java
 Code of stack     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 what is java     -  
 Java course     -  
 What is Java API?!!!     -  
 java or .net     -  
 need help in java     -  
 Using FTP in java     -  
 openGL in java     -  
 Fonts in java     -  



Topic Tags

Java Collections






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com