N/APosted on - 03/11/2015
Hi! I am a computer science student and recently I learnt recursion which is a very important concept in Programming. So I learnt this thing that in computer science and in recursion as well, the famous data structure 'Stack' is used. Whenever a function is used or the principles of recursion are applied, stack is used. My question is that is it possible that we could have done it by using queue rather than stack? I mean I know programming languages are designed in such a way to implement the functionality of stack but can we design a language which actually used queue in all these functions?
Using queue rather than stack?
The use of stack or queue is depend on the functionality and need in a program. Let’s consider on your desk at work contains various jobs for you to do in the near future. Each time another job comes in, you put it on top of the pile. When you finish one job, you take another job from the pile to work on. Which one do you take?
You can imagine this situation in software with an in-out data structure, an object that contains several elements, allows you to add, remove elements to it. You can do it in three different ways.
1. If data is taken on top, its Last-In-First-Out principle: A data structure that implements this principle is called a stack. The data is in the pile for the shortest period of time.
2. If data is taken on bottom, its First-In-First-Out principle: A data structure that implements this principle is called a queue. The data is in the pile for the longest period of time.
3. If data is taken according to the highest priority, its Highest Priority principle: A data structure that implements this principle is called a priority queue. Always take the job in the pile that has the highest priority.
Hence, when you do programming you should know what is the need and functionality you want to perform and choose the best to do it.