javastackqueue

Java Stack Linked list


I have declared a Queue of objects Node with the following lines of code:

Queue<Node> queue;
queue = new LinkedList<Node>();

However, when I declare a stack of the Node objects, by replacing the Queue with stack, it doesn't work. Why is it so? Also, what exactly does

queue = new LinkedList<Node>(); 

mean? Does it mean that a linked list of Node objects is being created and can be in the Queue?

I am taking open courseware to learn about Data Structures and Algorithms and I am a beginner. Thanks!


Solution

  • This is because java.util.LinkedList implements java.util.Queue but it is not a java.util.Stack though it has push and pop methods. Stack is a legacy class and its usage is not recommended, but if you still want to use it, this is the way to go

    Stack<Node> stack = new Stack<Node>();