0

I am practicing with stacks and queues and have some questions about them (mostly on queues)

How would I implement a queue in my code?

package *****;

import java.util.*;

public class stackPractice {

    /**
    * @param args
    */
    public static void main(String[] args) {
        Stack st = new Stack();
        Queue q = new Queue();

        st.push(100);
        st.push(90);
        st.push(70);

        System.out.println(st);

        //st.pop();

        System.out.println(st.pop());
        System.out.println(st);
        System.out.println(st.peek());

        //value = st.peek();
    }

}

I got Stack st to work as a stack, but Queue is giving me problems

on the 2nd Queue after new, there is a red squiggly line that says "Cannot instantiate the type Queue".

Queue q = new *Queue*();

I am not sure what that means.

---edit---

I know there is no actual code for the queue to do anything yet (enqueue, dequeue, etc...).

3
  • 1
    Queue is an interface, you cannot instantiate it. Commented Oct 23, 2013 at 17:35
  • Fab, can you restate your comment as an answer? Commented Oct 23, 2013 at 17:36
  • You are probably referring to the built-in Queue interface instead of your own implementation. Fix your imports. Commented Oct 23, 2013 at 17:36

4 Answers 4

1

Stack is a class in Java, but Queue is an interface, so you can't instantiate it. You'll need to call the constructor of one of its implementing classes.

Sign up to request clarification or add additional context in comments.

Comments

0

Queue is an interface so you cannot instantiate it but you need to "implement" it.

See http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html

Duplicate here: How do I instantiate a Queue object in java?

Comments

0

Queue is an interface and can not be instanciated.

You could use a LinkedList. or one of the listened:

   AbstractQueue, ArrayBlockingQueue, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, SynchronousQueue

Comments

0

Queue is an interface and interfaces cannot be instantiated directly. Use one of its implementations to create an instance of the interface

Queue<String> q = new LinkedList<String>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.