January 13, 2010

Example of Java Stack Program

Stack is like a bucket we you can enter objects and retrieve it. Here in the example describes the methods to prepare an example of Java stack program. The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector and implements Cloneable, Collection, List, Serializable interfaces. We mostly use push and pop operations.

In this example we are using two methods of Stack.
push(Object item): It pushes an item onto the top of this stack.
pop(): Removes the object from the top of this stack and returns that object as the value of this function. It throws EmptyStackException if the stack is empty.
Code of the program is given below
import java.util.*;

public class StackDemo{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new Integer(10));
stack.push("a");
System.out.println("The contents of Stack is" + stack);
System.out.println("The size of an Stack is" + stack.size());
System.out.println("The number poped out is" + stack.pop());
System.out.println("The number poped out is " + stack.pop());
//System.out.println("The number poped out is" + stack.pop());
System.out.println("The contents of stack is" + stack);
System.out.println("The size of an stack is" + stack.size());
}}
In this section, you will learn how to implement a stack in Java. A Stack is like a bucket in which you can put elements one-by-one in sequence and retrieve elements from the bucket according to the sequence of the last entered element. Stack is a collection of data and follows the LIFO (Last in, first out) rule that mean you can insert the elements one-by-one in sequence and the last inserted element can be retrieved at once from the bucket. Elements are inserted and retrieved to/from the stack through the push() and pop() method.

This program implements a stack and follows the LIFO rule. It asks you for the number of elements which have to be entered in the stack and then it takes elements for insertion in the stack. All the elements present in the stack are shown from the last inserted element to the first inserted element.
Stack():
Above constructor of the Stack class creates a empty stack which holds the integer type value which is mention with the creation of stack. The Stack class extends the Vector class and both classes are implemented from the java.util.*; package.
Stack.push(Object obj):
Above method is used to insert or push the data or element in the stack. It takes an object like: data or elements and push its onto the top of the stack.
Stack.pop():
This is the method to removes the objects like: data or elements at the top positions of stack.
Here is the code of program:
import java.io.*;
import java.util.*;
public class StackImplement{
Stack stack;
String str;
int num, n;
public static void main(String[] args){
StackImplement q = new StackImplement();
}
public StackImplement(){
try{
stack = new Stack();
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(ir);
System.out.print("Enter number of elements : ");
str = bf.readLine();
num = Integer.parseInt(str);
for(int i = 1; i <= num; i++){
System.out.print("Enter elements : ");
str = bf.readLine();
n = Integer.parseInt(str);
stack.push(n);
}}
catch(IOException e){}
System.out.print("Retrieved elements from the stack : ");
while (!stack.empty()){
System.out.print(stack.pop() + " ");
}}}

1 comment:

  1. Thus, here are some tips for everyone to be aware
    of on how to choose a car rental (oto kiralama) service and have a hassle free vacation.
    This is particularly important when a police report is not
    filed. Keep in mind that more landlords are now requiring tenants to maintain renters insurance and this
    amount is over and above your rental expense.



    Take a look at my blog post: location voiture Casablanca

    ReplyDelete

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts