Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


shilpi
stack
3 Answer(s)      6 years and 3 months ago
Posted in : Java Beginners



Advertisement
View Answers

October 13, 2008 at 10:13 AM


Hi friend,


Code to solve the Problem :

import java.io.*;
import java.util.*;

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

For more information on Stack visit to :

Thanks


October 13, 2008 at 10:13 AM




October 13, 2008 at 11:32 AM


I think there is bit wrong in the above answer.

the bugged code

import java.io.*;
import java.util.*;

public class StackExample{
Stack<Integer> stack;
String str;
int num, n;
public static void main(String[] args){
StackExample si = new StackExample();
}

StackExample(){
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() + " ");
}
}
}


Related Tutorials/Questions & Answers:
Stack
Stack  How to implement a stack using classes in java?   Here is an example that implements a Stack class based on arrays. public class Stack { private int top; private int[] storage; Stack(int
Stack
Stack Overview Stack follows the rule of last in first out rule. Mainly two action are performed by stack one is push and other is pop. The last thing which we placed or push on stack is the first thing we can get when we pop. A stack
Advertisements
Define stack
Define stack  hii, Explain stack
stack in java/
stack in java/  java code for stack push pop and display operation? thanks
Define stack
Define stack  hii, Explain stack?   hello, Stack is an ordered list in which all the insertions and deletions made at one end called top of the stack. It is referred as LIFO list. Last inserted item is deleted First
ARRAY and STACK
and STACK?   hii, STACK follows LIFO. Thus the item that is first entered... order.   a stack is simply a special case of an array. You could say that a stack uses user defined methods to remove and add contents to an array
Array stack
Array stack  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
Array stack
Array stack  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
Stack and Queue
Stack and Queue  Consider a class called Configuration. This class √Ę‚?¨Ň?encapsulates√Ę‚?¨¬? the configuration management for any software system... or a Stack. Your App class is given and you should not change this class at all. Your
Stack Overflow - Java Tutorials
Stack Overflow in Java A stack is the part of the memory. The local automatic variable is created on this stack and method arguments are passed. When a process starts, it get a default stack size which is fixed for each process. 
Java Stack
Java Stack       The Stack class works on the principle last-in-first-out (LIFO) stack of objects... a vector to be treated as a stack. Methods push() and pop() are used to pick
What is a stack overflow error?
What is a stack overflow error?  What is a stack overflow error
java technologies stack
java technologies stack  java technologies stack
stack using linked list
stack using linked list  how to implement stack using linked list
heap and stack memory
heap and stack memory   hi i am jane pls explain the difference between heap memory and stack memory with example programs
Java collection Stack example
Java collection Stack example  How to use Stack class in java collection?   The Stack class uses the First In Last Out(FILO) mechanism. Here is an example of Stack class. import java.util.Stack; public class
can Stack accept imageIcon ..??
can Stack accept imageIcon ..??  how can write program like album ..by using Stack as an ideas datastracture and using imageIconb ?? please help me ..thanks alot
Web service protocol stack
Web service protocol stack  hii, What is the Web service protocol stack?   hello, The Web service protocol stack is an evolving set of protocols used to define, discover, and implement Web services
remove element from stack
on the internet how to remove names from stack by asking user how many names they want to remove and if the number they enter is bigger than the stack number then show error message and if not then remove the names from stack until the stack is empty
stack and queue - Java Beginners
stack and queue  write two different program in java 1.) stack 2.) queue   Hi Friend, Please visit the following links: http://www.roseindia.net/java/example/java/util/StackImplement.shtml http
Java stack help
Java stack help  How would I use stacks in java to do a calculation like: 4*((3+3)/(5-3
heap and stack memory
heap and stack memory   hi i am jane pls explain the difference between heap memory and stack memory   STACK memory is referred as temporary memory,if you come out of the program the memory
stack - Java Beginners
that inputs a line of text and uses a stack object to print the words of the line... : import java.io.*; import java.util.*; public class StackExample{ Stack stack...(){ try{ stack = new Stack(); InputStreamReader ir = new InputStreamReader
Java Stack - Java Beginners
Java Stack  Can you give me a code using Java String STACK using the parenthesis symbol ( ) the user will be the one to input parenthesis......) if Open parenthesis push it to stack otherwise Pop the stack Thank you
Generic Java Stack
- package jp.co.worksap.intern; /** *The Stack class represents a last-in-first-out(LIFO) stack of objects *And this class can look at the object which has the highest (lowest or so) value. *So every object onto the stack
Java Stack Example
Java Stack Example In this section you will learn about Stack class  in java, how to use Stack in java. Before going into details we should know what is stack class, stack  class represent Last In First Out(LIFO) of object
{create stack with queue}
{create stack with queue}  how can create queue that accept all data type ?? meanse that this array is used accept all type for example(int ,char,String,float).?? please help me ....which output is : 1- to create queue 2-Equeue
Collection : Stack Example
Collection : Stack Example In this tutorial we will describe implementation of stack with example. Stack : Stack is based on concept of LIFO(Last In First... one access point that is top of stack. There are two main methods of stack
ADT stack - Java Beginners
ADT stack  Design a Java interface for ADT Stack. Develop two....  Hi Friend, Try the following codes: ADT Stack: import...("Stack is empty"); return; } else { String str = " "; for (int i = 0; i <= top
heap and stack in general programming language
heap and stack in general programming language  what's the difference between heap and stack ?   Hi Friend, Differences: 1)Stack is referred as temporary memory while Heap is referred as permanent memory. 2)In stack
ADT stack - Java Beginners
]. 2)Design a Java interface for ADT Stack. Develop two different classes
Advertisements
 

 

 

DMCA.com