1/6/2024 0 Comments Queue fifo![]() Of course I went with Redis, taking advantage of two main commands: RPUSH and LPOP. Database, creating a table, but I had also to create a Model, Service, Repository and many other things.This is my list in which I add a node at end of the list.Recently I had the need to create a FIFO (First In, First Out) queue in Laravel to run specific actions inside a long-running worker from outside the worker itself. So you may want to seek out authorative work on the subject.ĮDIT: just googling around. There are well know solutions for things like Qs that they devised with "best practice" for how to implement efficiently (like not needing to keep rewalking the list to get from Front to Back or Back to Front etc). ) and others worked this all our so that, on the whole, none of us have to reinvent the wheel. Or, as explained by Andy and me above, you don't actually need "Back" as you can always start at "Front" and walk the list to the back to get to the end where you add new entries.īy the way, back in the mists of time of computing (from the early part of last century onwards) greater minds than hours like Djikstra (. New elements are added at the Back and the oldest is used (and removed) from the Front. In this case you only really need Front and Back pointers. If you simply make the queue dynamic in length then you just keep adding (malloc) and deleting (free) entries as elements are added. But if you have Start/End, Front/Back pointers (and Back is kept up to date!) it can save you doing the long walk.Īs for Write and Read pointers - that's only really necessary if you are going to pre-allocate a queue of a given length with "empty" slots at the start and need to keep track of where the next one will be written and where the oldest one will be read from. ![]() I found that I need to use two pointer to make FIFO queue, one pointer will add the person in a queue and another pointer will remove the person from the queue.Īwneil wrote: Otherwise, you would have to "walk" the queue to find the end.It's not diifcult to "walk" a list but, depending on list length it can be time consuming:Īt the end of that pItem points to the last one in the list. Printf("Rear item is %d\n", rear(queue)) Printf("Front item is %d\n", front(queue)) Driver program to test above functions./ Function to remove an item from queue. Void enqueue(struct Queue* queue, int item) Struct Queue* queue = (struct Queue*)malloc( Struct Queue* createQueue(unsigned capacity) C program for array implementation of queue I don't work with c++ question is only for learning I need to make FIFO Queue something like this h)īut to answer your general programming question:īut the fun will come when you want to add something to this:īut this obviously is in balanced in that "myQ" is an element that is statically allocated at the start then pNew (and the ones that follow) are dynamic allocation so what you normally would do is have them all dynamically allocated:īTW, if this is PC code, you may want to consider using C++ as the Standard Template Library has support for just about any common, computing science data structure there is, on the whole you never need to write any of this kind of stuff as it's all there on a plate for you. ![]() If it's simply a fifo (and not a queue as your naming suggests) then maybe take a look at how it was done here:Ĭode.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |