Stack and Queue

Thread Solved

Join Date: Feb 2009
Posts: 98
Reputation: weblover is an unknown quantity at this point 
Solved Threads: 1
weblover weblover is offline Offline
Junior Poster in Training

Stack and Queue

 
0
  #1
Jun 14th, 2009
hi all ...how are u? i have a question ...how can i output the items of a java Stack ...is there any predifine method??
and another question ...how can i use the class Queue without interface ? ...because i'm typing in my code (Queue q =new Queue()
but it's giving me an exception ...what should i do ??

thnx alot in advance.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: rcollins is an unknown quantity at this point 
Solved Threads: 3
rcollins rcollins is offline Offline
Newbie Poster

Re: Stack and Queue

 
0
  #2
Jun 14th, 2009
My suggestion is to google Java API. http://java.sun.com/j2se/1.5.0/docs/api/
Look up Stack. It will list all the methods available. It will also show you other methods inherited from other classes. Of interest to you till be List which has iterators which will allow you you iterate through the elements of the Stack.
Look up Queue. It is nothing more than an interface so either you have to implement a Queue or choose a class that implements one for you. When you look up Queue it actually list all known implementing classes one of these classes might work better for you.
Give this a shot and come back with any questions. Gook luck!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 98
Reputation: weblover is an unknown quantity at this point 
Solved Threads: 1
weblover weblover is offline Offline
Junior Poster in Training

Re: Stack and Queue

 
0
  #3
Jun 14th, 2009
i looked up the api ....and there is no method that let's u output the element of a stack ....and for the Queue where can i find a ready class for it ....thnx for help
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: rcollins is an unknown quantity at this point 
Solved Threads: 3
rcollins rcollins is offline Offline
Newbie Poster

Re: Stack and Queue

 
0
  #4
Jun 14th, 2009
For outputting the elements of a Stack you aren't going to use a method you are going to use iterators to go through the stack element by element. Do a search online to get some info about iterators. As for the Queue look at the API page. It has a list of Implementing classes. This means that these classes are effectivly queues. Use one of them if you don't want to implement the code yourself.
Last edited by rcollins; Jun 14th, 2009 at 6:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Stack and Queue

 
0
  #5
Jun 14th, 2009
Look at the java tutorial on collections there is an entire trail there, here is a link: Collections trail
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Stack and Queue

 
0
  #6
Jun 15th, 2009
Originally Posted by weblover View Post
i looked up the api ....and there is no method that let's u output the element of a stack ....and for the Queue where can i find a ready class for it ....thnx for help
Oh, there isn't? Because by typing in "Stack java" to google I found this link. Hm
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Stack and Queue

 
0
  #7
Jun 15th, 2009
Because a Stack can contain any kind of Objects, of unlimited complexity, there's no sensible way to provide a generic "print" method that would work usefully for everything that could be in the stack. Hence all the previous advice saying that you have to loop thru all the elements in the Stack printing each element in some way that makes sense for the type of each element.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 98
Reputation: weblover is an unknown quantity at this point 
Solved Threads: 1
weblover weblover is offline Offline
Junior Poster in Training

Re: Stack and Queue

 
0
  #8
Jun 15th, 2009
thnx all ...but for the iterator ........when i can let the while stop ....i tried i can print them in a while but i don't know in what condition can i stop the while ....
and for interfaces ...i don't know how to use them ..i did not use them anytime ...can't i find a class queue without interface ?
i found out a way to print the stack elements ..but it removes all the element of it ...is there another way ?
thnx in advance.
Last edited by weblover; Jun 15th, 2009 at 10:26 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Stack and Queue

 
0
  #9
Jun 15th, 2009
Easiest way to iterate thru all the objects in a Stack is like this:

  1. for (Object o : myStack) {
  2. System.out.println(o); // or whatever
  3. }

An interface is set of public methods that a class must implement. For example, the Queue interface specifies methods called element, offer, peek, poll, remove. If a class is declared as implements Queue then it must implement all these methods exactly as defined in the Queue interface. The compiler then knows that you can call peek, poll etc on members of that class. There are a number of classes on the API that implement the Queue interface, such as LinkedList or PriorityQueue - all of these have all the Queue methods, and thus be used to hold a queue.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 98
Reputation: weblover is an unknown quantity at this point 
Solved Threads: 1
weblover weblover is offline Offline
Junior Poster in Training

Re: Stack and Queue

 
0
  #10
Jun 15th, 2009
thnx alot james for your help ..my stack contains strings ....i fill it with strings exp: t.push("abd") how can i iterate it like this ???
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC