Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Implementing a Stack in Java 
 

In this section, you will learn how to implement a stack in Java.

 

Implementing a Stack in Java

                         

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<Integer>(): 
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<Integer> stack;
  String str;
  int num, n;
  public static void main(String[] args){
    StackImplement q = new StackImplement();
  }
  public StackImplement(){
    try{
    stack = new Stack<Integer>();
    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() + "  ");
    }
  }
}

Download this example

                         

» View all related tutorials
Related Tags: c string reference io method type boolean key rmi ole this ai node oo create if boo example to contains

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

u have very build up knowlage good luck..........

Posted by riz on Monday, 09.8.08 @ 14:24pm | #77983

Thanks for this program code. It help me a lot on my assignment!!!! THANKS FOR ROSEINDIA!!!

Posted by Larry Lozada on Friday, 04.18.08 @ 07:19am | #57008

Hi Guys,

I am Paramjit from india.
I want learn Java Programming.
I am having very godd logic understanding,
and devlopment experience in Visual Basic
Programming. But I want to switch to Java
Programming.
Can you help me to start

Thanks & Regards
Paramjit

Posted by Paramjit on Friday, 08.10.07 @ 20:59pm | #23112

stack.push(n) in this we should pass the object but here we are using primitive..then i am trying to use got compile time error

str = br.readLine();
n=Integer.parseInt(str);
stack.push(n);

this is not correct..........


str = br.readLine();
n=Integer.parseInt(str);
Integer n1=new Integer(n);
stack.push(n1);
this is correct

Posted by gangadhar bommasani on Wednesday, 06.27.07 @ 13:21pm | #20271

very very very good source code

t

Posted by aman on Sunday, 04.15.07 @ 21:11pm | #14258

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.