Tuesday, April 21, 2009

Linear Data Structures :: Creating Queues

The concept of a queue is what every small child learns when they go to school. Wait your turn. The queue is used to create a FIFO list also known as first in first out. A queue is similar to a stack in that the top of the stack is like the front of the queue. However, the stack is created using Push at the Top while the queue is created by inserting the new node at the back of the queue.

STACK
The following image is an example of a stack. It shows the TOP of the stack equal to 3. The image demonstrates how the stack would push new nodes. Each new node would push down on the next creating what would look like a tower of blocks. The problem with a stack is that if data at the bottom of the stack needs to be popped off the stack it has to wait for everything else to go first. This is a problem in programs that create stacks of processes. Starvation is the term used for a process that does not get a chance to execute.


QUEUE
The image of the queue can be viewed as a line of people waiting at a bank. The first person in line would get to go first. After that person completes their transaction they can leave the queue and the next person can be in front of the line. A queue is a good way to ensure that everyone gets treated equally and prevents starvation from occuring.

No comments:

Post a Comment