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.

Recommended Answers

All 17 Replies

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!

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

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.

Look at the java tutorial on collections there is an entire trail there, here is a link: Collections trail

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

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.

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.

Easiest way to iterate thru all the objects in a Stack is like this:

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

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.

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 ???:S:S

Show me the declaration of your Stack - how you declare it affects how you use it.

that's my code ...i tried to make these function works ..but only the first one works and the other gives me exceptions ...and thnx in advance for help ..

import java.util.*;
   import javax.swing.*;
   import java.io.Serializable.*;
   import java.util.LinkedList.*;


           
   abstract class test  implements Queue{
           
   
              
      public static void deletelastelemetstack(Stack q){
              
      
         Stack t1 = new Stack();
         //Queue ee=new Queue();
      
         if(q.empty() == true)
            JOptionPane.showMessageDialog(null , "Stack is empty");
         
         else {
         
            while(q.empty() != true){
               t1.push(q.pop());
            }
            t1.pop();
         
            while(t1.empty() != true)
               q.push(t1.pop());
         
         }
      
      }
   //	public static void 
              
      public static void print(Stack e ){
              
         while(e.empty() != true)
            System.out.println(e.pop() + "\n");
      
      }
              
      public static void print1(LinkedList p ){
              
      
         while(p.size() != 0)
            System.out.println(p.removeLast() + "\n");
      
      }
   
              
      public static void inverse(Stack t){
              
         LinkedList e = new LinkedList();
         while(t != null)
            e.addFirst(t.pop());
      
         while(e != null)
            t.push(e.removeLast());
      
      
      }
              
      public static void deletelastelementqueue(LinkedList te){
              
         Stack t = new Stack();
         Stack t1=new Stack();
         while(te != null)
            t.push(te.removeFirst());
         t.pop();
         while(t != null)
            t1.push(t.pop());
      
         while(t1 != null)
            te.addFirst(t1.pop());
      
      
      
      
      
      
      
      }
   
   
   
              
      public static void main(String []args){
              
        // Stack test = new Stack();
      	//Stack test1 = new Stack();
      
         /*test.push("ahmad");
         test.push("3ali");
         test.push("aaa");
         test.push("bb");
        // afficher(test);
         //supprimerbaspile(test);
         try{
            inverser(test);
         }
            catch(Exception e){
            }
         afficher(test);*/
         LinkedList t =  new LinkedList();
         t.addFirst("asd");
         t.addFirst("adsfas");
         t.addFirst("abd");
      
      
      
      
      }
   
   }

What exceptions? Where exactly?

the exception is (Nosuchelement exception) and it appears when i trid to execute these functions (inverse) and (deletelastelementqueue)

the exception is (Nosuchelement exception) and it appears when i trid to execute these functions (inverse) and (deletelastelementqueue)

In your inverse function you are testing for t != null even after you pop the last element from stack t it will still be a stack and therefore won't be equal to null. You want to use the boolean empty() method to test if the stack is empty. You are going to have the same problem with the next while loop where you test e the same way.

Same idea with your deletelastelementqueue method. You test your linkedlist with te != null you want the boolean isEmpty() method it inherits from List . I hope this helps.

thnx alot james .......it works .....thnx for your help

That's OK, but I think rcollins deserves thanks too!

thnx for all who helped me ......and sorry for forgeting rcollins .....thank you all alot .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.