# Recursion + Stack/Queue usage in practical life implementation [closed]

I am very new to Data Structures and Algorithms. I want to use recursion + stack/queue in a grocery store inventory program that I am making. However, I can't come up with any good ways of using recursion in a manner that contributes and is relevant to my grocery store inventory system.

I know recursion can be used for the tower of hanoi, coming up with palindromes and other random things like making patterns. However, I am not sure how to apply it to a grocery store inventory system such that it provides value to the staff who interacts with the program.

I don't know how to implement recursion and I just need ideas on what I can possibly do with it, in a way that adds value to my grocery store inventory application. Would an order system be able to use recursion in an effective way?

• Recursion is just a tool to solve certain classes of problems where a solution can be assembled from sub-solutions. Often, the solution can be expressed more easily with a for loop. Most line of business software doesn't feature lots of algorithms, those are already taken care of by the database. So don't try to stuff extra algos into a software that doesn't need them, and instead focus on the user problems. – amon May 26 at 17:04
• Welcome to Stack Exchange! What specifically are you having trouble with? Your question is very broad, which makes it a bad fit for this site. Can you narrow it down to one particular problem you're having with recursion and include the details of that problem? – mmathis May 26 at 17:05
• @mmathis thank you. just updated it. essentially I need to use recursion in some way in my grocery store inventory program but I don't have any ideas on what I should do with recursion. Should an ordering system be made? Will an ordering system be able to apply recursion effectively? – potatoooooooooo May 26 at 17:08
• Is this a homework assignment? Note that any loop can be expressed using recursion instead of a loop form, so maybe take one of your for loops or while loops and write it recursively? – Robert Harvey May 26 at 17:16
• @potatoooooooooo You can transform any occurrence of a loop into recursion, though the result might not be particularly elegant. For example: function sum(list) { int s = 0; for (int x in list) s += x; return s} = function sum(list) { if (list is empty) return 0 else return list[0] + sum(list[1:]) } – amon May 26 at 17:16

One way to use recursion would be a product search function, using a simple Binary Search Tree or something similar to store the data. https://en.wikipedia.org/wiki/Binary_search_tree

Adding, removing, and modifying inventory items would be fairly simple and something commonly covered in a Data Structures class.

As far as using a queue, you could build a list of inventory changes with the form (product_id,count_change) that you could process through using your search feature discussed earlier.

Also, the ability to read the inventory from a text file and write it back out would easily exercise these concepts and make it easier to identify bugs as you are developing.

An inventory system doesn't seem to offer a lot of opportunities for this kind of thing. If you are doing this for work, I would agree with amon that trying to force in such a solution is not really a great idea. It will devalue the solution that you are producing. Recursion can come with some performance penalties as well, depending on a number of factors. When it's good fit, though, it can really simplify things.

The best I can come up with for this at the moment would be a restocking algorithm. Let's say a store has run out of something e.g. toilet paper. You need to try to find the closest warehouses and/or distributors that can provide you with some.

You could implement this with a recursive strategy. To start, you need a tree with a single root node. Under this, you could add 'distributors' and 'warehouses'. Under that, you have increasingly smaller subsets of each. This could be based on physical location (which is probably what you want for your own warehouses.) For distributors, maybe you might want to split it up by both company and location.

Probably, you can solve this adequately with just a straight-forward loop. The main benefit of a recursive strategy here would be if you want to parallelize the search. To make that worthwhile, the search space would need to be pretty large, most likely.