'stack vs queuing?

hello im still a student and im a bit confused about stacking and queuing ? first question is,

  1. what is the main diffrence between them two ?

  2. btw there is circular queuing beside normal queuing how about that ? how do they work ? is there any different ways to queuing?

  3. im useing php, is there a simple ( very simple or easy to read ) sample code that i can learn on ( links are okay too. )?

  4. there is pop, push and etc ( stacking and queuing ), is there anything like that in php ?

Thank you very much for looking in.



Solution 1:[1]

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

Solution 2:[2]

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_push - put a new element at end of array
  • array_pop - remove an element from end of array
  • array_shift - remove an element from the beginning of array
  • array_unshift - put a new element onto the beginning of array.

  • For a stack you'd use array_push and array_pop

  • For a queue you'd use array_push and array_shift

A circular buffer I would implement as a standalone object.

Solution 3:[3]

It appears you are being steered towards the difference between first-in first-out and last-in first-out queues. A stack is the former, and a circular queue is an efficient implementation of the latter.

  1. A stack is a LIFO (last in, first out) queue.

  2. You can have a circular queue - these were most common on communication interface buffers as they had limited memory with data coming in asynchronously and the data being read by the CPU at different times.

  3. You can create queues in any language if you know your language well.

  4. The php website offers a lot of documentation.

Solution 4:[4]

A stack adds and removes items from the same end.

A queue adds items to the back and removes items from the front (like a line in a bank.)

There's an article about them both that explains in detail with code samples.

A circular buffer has limited space and keeps adding items in a circular fashion overwriting the ones at the end.

Solution 5:[5]

Solution 6:[6]

This is only an answer for the 1st question, and the post is very old, but still wanted to share, because i like this image even though test tubes are usually for liquids :)

queue vs stack

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 thomasmalt
Solution 3 PP.
Solution 4 JoshD
Solution 5 Jase Whatson
Solution 6 murtiko