Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 2 3 4 5
Each car arrives to get through the traffic lights. The size of the queue is the number
of cars in the queue.
Cars 1 and 2 get through the lights. Another 4 cars come along, Car 6, Car 7, Car 8
and Car 9. What does the queue look like?
6 7 8 9
Front Rear
Page 1 of 6
You could draw this queue like this:
103
102
101
100
99 Tail (Back) Address of the last item in the
queue
9
98
8
97
7
96 Head (Front)
What is the memory address of the first item to leave this queue? ________
Unlike real life queues, if an item leaves a memory queue the Head pointer is
changed to the new start of the queue.
Assume that two items leave the car queue above – where is the Head pointer now?
Assume that three new cars join the queue – where is the Tail pointer now?
Once a queue fills up to the maximum amount of data addresses it becomes unusable
as you run out of space.
Page 2 of 6
Where are queues used in computing?
1. The keyboard – when you type each character goes into a queue in the
keyboard buffer
2. Printer jobs are held in FIFO queues
3. Simulations of traffic, supermarket queues – to help each congestion/put in the
right number of supermarket checkouts.
Queues aren’t simple data types – and often need to be implemented by the
programmer in a particular language if they are needed. For AS you don’t need to
know how to implement this – it’s an A2 skill.
Page 3 of 6
Shelf Stackers – Learning about STACKS
Chocolate Box 3
2
Chocolate Box 2
1
Chocolate Box 1
Items DON’T move up or down the stack if you PUSH or POP them. What happens
is that the STACK POINTER moves up or down to indicate the new top of the stack.
Initialising a stack resets it – and clears out any data. The pointer is set to _______
after initialisation to indicate an empty stack.
You can implement a stack by using an array to hold the stack data – and two integer
variables. One variable will hold the size of the array (i.e. how big the stack can get)
and the other stores the position of the top of the stack (i.e. the pointer).
Stacks are used to perform calculations, and in transferring control from one part of a
program to another.
POP something
Let’s say that we are now going to POP the stack of chocolate boxes – and place each
box in a different packet – ready to be delivered on Valentine’s Day.
Page 4 of 6
In order to get each box into the right packet we need to add something to the POP
instruction – i.e. POP where_to_pop_to.
Let’s say we label each packet as follows: London, Oxford, New York.
What instructions would we need to write in order to get “Chocolate Box 2” into the
New York packet, and “Chocolate Box 3” into the London packet.
Step 1:
Step 2:
Step 3:
POP each car one by one – and put contents back into the queue, one after the other.
The front of the queue is now the last car pushed onto the stack (i.e. car 5) – and the
back of the queue is now the first car pushed onto the stack (i.e. car 1).
If you try to add an element to a queue or stack that has no free space (i.e. is full) then
you get _____________ .
If you try to remove an element from a queue or stack that has no data (i.e. is empty)
then you get ____________ .
http://www.cmpe.boun.edu.tr/~akin/cmpe223/stack_queue/chap2_2.htm
and
Page 5 of 6
P:\PayneGal\BookBase.exe
Describe how the elements in a non-empty queue are reversed with the aid of a stack.
Page 6 of 6