Stacks Using Queues
Computer science involves a lot of layering which is called abstraction in technical terms. Stacks and queues are one such layer of data structure. They contain a specific set of rules which define data structure. So before we get deeper, let’s understand stack and queue first.
What is a stack?
A Stack is a linear data structure. It is very much like a stack of trays. Like the stack of trays, you can insert or delete elements only from one side. It follows the last in first out like a tray stack which means that the element inserted, at last, comes out first. It is a limited access data structure.
The process of inserting an element is called push and the process of deleting an element is called pop operation.
What is Queue?
A queue is a linear form of data structure in which the elements can be inserted from one side, called the rear and removed from the other side, called the front end. You can insert an element from one side and delete an element from the other side only. It applies the first in, first out principle like a queue at the mall for popcorn.
The process of inserting an element is called enqueue operation and deleting is called dequeue operation.
Why use stack and queue in data structure?
Modelling and abstracting are very important in computer science as they can turn a machine into anything we want, which otherwise would only be doing 1s and 0s. Stacks and queues are abstractions to have control over the data in a different manner by adding another layer of rules to the pre-existing structure.
Stacks and queues are thus widely used in computer design as they give linear instructions for execution in the data structure.
Implementation of stack and queue
There are two ways of implementing stack using queue:
- By making push operation costly
- By making pop operation costly
Well, these are topics to be understood in depth which will take us another separate blog. For now, let’s focus on the basic understanding of stack and queue.
Stack and Queue Differences
The following differences between the stack and queue will help you understand them better.
- Basics: Both stack and queue are linear data structures but in Stack, elements are inserted or deleted from the same end i.e. top. Queue, on the other hand, follows the first in, first out principle where elements can be inserted from the top and removed from the end.
- Pointers: The stack has only one pointer–The Top which indicates the topmost element of the stack. The queue has two pointers, one at both the ends known as the front and the rear. The rear indicates where the element is inserted and the front indicates where the first inserted element or from the element would be removed.
- Structure: In a stack, inserting and removing an element takes place from the same point. In Queue, inserting is done from the top point which is the rear point and removal of elements takes place from the front point or the front end of the queue where the first inserted element is.
- Variants: Stack has no variants while Queue has 3 types viz. Circular, priority and double-ended queue.
- Implementation: Stack has a simple implementation while Queue is a little complex when compared to stack
Stack and queues are excellent data structures which represent the power of computer science very well. They are built on the previously developed structure to create a new structure and solve a problem. This might look a little complicated right now but when you dive deeper into understanding these, one by one and in detail, they are very interesting and intriguing. So, go ahead and start learning more about stack and queues and if you need help with learning, you can always look up to Geekster for guidance and courses.