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...).

dckuehn
  • 2,281
  • 3
  • 25
  • 35
PYC
  • 1

4 Answers4

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.

Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861
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>();
Reimeus
  • 155,977
  • 14
  • 207
  • 269
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?

Community
  • 1
  • 1
Fabien Sa
  • 8,378
  • 2
  • 36
  • 42
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
MemLeak
  • 4,228
  • 4
  • 41
  • 80