Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





Java - Code Samples

 Sample 1. Write a Program to implement stack using an array

public class Stack{
static int top = 0;
static Element[] stack = new Element[10];

static class Element {
int body;

Element(int value){
body = value;
}
}

public static void main(String[] args){
push(5);
System.out.println(top);
push(7);
System.out.println(top);
poll();
System.out.println(top);
poll();
System.out.println(top);
}

private static void push(int value){
Element newElement = new Element(value);
top++;
}

private static Element poll(){
Element topElement = stack[top];
top--;
return topElement;
}
}

   Like      Feedback     stack



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus