*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.
Stack vs queue: what’s the difference?
Both stacks and queues are abstract data types that are linear lists of elements that can be created and then used in a particular order. Depending on how you plan on using the data, one or both may be appropriate. We’ll explain the difference here and talk about how they can both be used.
What Are Stack vs Queue?
A stack is a linear data structure abstract data type that follows a last-in first-out (LIFO) use case. Conversely, a queue is another linear data structure abstract data type that follows a first-in first-out (FIFO) use case. So in learning how to differentiate stack vs queue, you might first benefit from looking at some real-world examples of both.
Stack – A Real-World Example
Here is a way to envision how a stack works. Consider the ‘Undo’ function in a word processor. You delete items in a particular order. Say you deleted the word ‘dog,’ then you deleted the word ‘cat,’ and then you deleted the word ‘cow.’ If you select ‘undo,’ the first word replaced in the document is the last word you deleted. Clicking undo the first time gives you the word cow in your document; the second click gives you cat, and so on.
Or think of a buffet line in a restaurant. The waitstaff puts a pile of dishes on top of the already existing stack. You remove them in the order in which they were placed. The last dish on the pile is the first one removed.
Queue – A Real-World Example
Here is a way to envision how a queue works. We encounter queues often in our daily lives. Picture yourself standing in line at a movie theater to buy a ticket. The first person in line is the first person to get a ticket. Other movie goers are served in the order in which they came into the line.
Most of the lines we encounter in our daily lives are queues. Think of calling your service provider’s customer support line. Remember those pleasant (not!) messages that say, ‘Calls will be answered in the order it was received?’ You are now stuck in a queue; if you’re lucky, not for long.
What Are Stack and Queue Used For?
Stack Vs Queue – Stack Usage
In using a stack, there are two basic operations: push and pop. A push operation adds a data element to the top of the stack, while a pop removes that element from the stack. Think again of our word processor’s undo operation. That is a perfect use for a stack.
A stack contains a pointer that is used to determine which element is the latest to be added to the stack. This pointer is called the top. Push and pop operations only occur at that top end of the structure.
Stack Vs Queue – Queue Usage
The queue data element works differently. Data elements can only be entered from the back of the group, which is called the rear. Conversely, data elements can only be removed from the front of the group, which is obviously named the front. Unlike stack, where only a top pointer is needed, queues have two pointers: the front pointer and the rear pointer.
The inserting of an element into a queue is called an enqueue operation, while removing an element from the front of the queue is called a dequeue operation.
Both data types also have a peek operation which allows the front element in the stack or the queue to be viewed or returned without removing it from the data structure.
How Can Stack and Queue Be Used Together?
Take and Place
Now, you will need to take the letters from the queue and place them into the new stack. Remember that a queue is FIFO (first-in first-out). So the first letter out is the A, which is placed at the top of the new stack. That letter is followed by the B, which goes on the top of the A in the stack. Your new stack now looks like this: BA.
Continue the operation until all five letters have been moved from the queue to the stack. Each letter that comes out of the queue is placed at the top of the stack, so when all five letters have been moved, the result is a stack with the letters in this order: EDCBA. You’ve done it!
Conclusion – Stack Vs Queue
A stack is LIFO (last-in first out). Stack operations are push and pop. A stack only has one pointer, called the top, which is at the front of the stack. Stacks also support peek operations that allow you to view the top of the stack without removing the top element from the stack.
A queue is FIFO (first-in first-out). Queue operations are enqueue, which places data elements into the stack, and dequeue, which removes data elements from the stack. A queue has two data pointers: a front pointer and a rear pointer. Queues also support a peek operation analogous to that of the stack.
Now that you understand the difference, you’ll be able to include these data structures in your programs. Good stacking and queueing!