Queues 2 (using array, code based version)
by Vangelis Livadiotis
The difference is that the array really gets "wrapped" meaning that when an element gets removed from the front, the front pointer points to the next element. That means that the front of the queue is not always the first element of the array. In the image found here, you see that when an element is removed the front points to the next element in the array. The back (tail) variable always holds the number of the place of the next available position.
This is quite a simple concept. The only slightly confusing bit may be the formulae q.back := (q.back + 1) mod capacity; used. When we start, for example, the initialize procedure sets both front and back (tail) pointers (integers to be exact) are set to 0. When we add an element we will add one to front and get one. Say the capacity (like in this example) is 8, 1 mod 8 is 1 since 8 does not go in to 1 and there is 1 remainder. The same applies for 2,3,..etc. Until is becomes 8, where the remainder is 0 since 8 divided by 8 is 1 and remainder is 0. Then, the pointer goes back to the beginning of the array. This formulae is applied to front and back pointers every time an element is added or removed respectively.
Note that the initialize procedure is given when the page is loaded so check the textbox before creating a queue.
The full Pascal code for a queue is given here.
To use simply fill the field with a string to add and click Add, or simply click Remove to remove the Front of the list.
Home | Arrays | Arrays2 | Queues1 | Queues2 | Stacks | Arguments | Pointers |
Binary Search Tree
* Code taken from Lecture 2 in Data Structures Semester 2 2005 by Mr. Chris Cox for Oxford Brookes University.
Copyright © June 2005 - Author Vangelis Livadiotis