Queue | Stack |
---|---|
First item to be inserted is the first one to be removed. | Allows access to only last item inserted. |
This mechanism is called First In First Out (FIFO). | An item is inserted or removed from one end called the “top” of the stack. This is called Last In First Out (LIFO) mechanism. |
Placing an item in the queue is called “enqueue or insertion” and removing an item from a queue is called “dequeue or deletion”. Pre J2SE 5.0, you should write your own Queue class with enqueue() and dequeue() methods using an ArrayList or a LinkedList class. J2SE 5.0 has a java.util.Queue interface. |
Placing the data at the top is called “pushing” and removing an item from the top is called “popping”. If you want to reverse “XYZ” -> ZYX, then you can use a java.util.Stack |
What is the difference between a stack and a queue?
Tagged Data Structure, Interview Questions and Answers, OOPs. Bookmark the permalink.