Is it possible to create a FIFO 'stack' in C without using 2 stacks?
Thanks!
(Sorry to those who answered the previous one. I was thinking LIFO and meaning FIFO.)
FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last.
A queue in C is basically a linear data structure to store and manipulate the data elements. It follows the order of First In First Out (FIFO). In queues, the first element entered into the array is the first element to be removed from the array.
No. The C11 standard does not contain the word stack , nor does it contain the word heap . That means it does not require either by name.
Queue: First In First Out (FIFO): The first object into a queue is the first object to leave the queue, used by a queue. Stack: First In Last Out (FILO): The first object or item in a stack is the last object or item to leave the stack.
It's very easy. Just implement a doubly-linked list, holding the pointer to the last item in the list.
To add to the queue, create a new node at the beginning, linking it to the previous beginning. (normal list insert)
To remove from the queue, deref the pointer to the last item, change the pointer to the previous-item pointer, and return the last item... (This is why the doubly-linked list. The other option is a singly-linked list and iterating the whole list to get pointers to the last two elements).
It sounds like you're trying to make a queue, where you insert at one end and pull from the other.
One way to do that is with a linked list. You can store pointers to the head and tail. When you insert, append the new record to the tail and when you pop something off the queue, you grab the node pointed to by the head pointer. (Or vice verse, it doesn't much matter)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With