hello im still a student and im a bit confused about stacking and queuing ? first question is,
what is the main diffrence between them two ?
btw there is circular queuing beside normal queuing how about that ? how do they work ? is there any different ways to queuing?
im useing php, is there a simple ( very simple or easy to read ) sample code that i can learn on ( links are okay too. )?
there is pop, push and etc ( stacking and queuing ), is there anything like that in php ?
Thank you very much for looking in.
Stacks are based on the LIFO principle, i.e., the element inserted at the last, is the first element to come out of the list. Queues are based on the FIFO principle, i.e., the element inserted at the first, is the first element to come out of the list.
Use a queue when you want to get things out in the order that you put them in. Use a stack when you want to get things out in the reverse order than you put them in.
Stack has a dynamic and fixed size. Queue can contain elements of different data type. Array contains elements of same data type. The stack can contain elements of the different data types.
1: While with stacks the insert/removal operations both work on the same end of the data structure (top)
with queues the insertion takes place at one end (rear) and the removal at the other end (front).
(Both images are from the respective wikipedia entries)
2: see http://en.wikipedia.org/wiki/Circular_buffer
3: and 4: see SplStack and SplQueue
In php you would use an array() to hold your data for both stacks and queues and use the array_* functions to manipulate them. Take a look at array functions at php.net
You have
array_unshift - put a new element onto the beginning of array.
For a stack you'd use array_push and array_pop
A circular buffer I would implement as a standalone object.
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